943,563 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 5974
  • C++ RSS
Sep 17th, 2008
0

Debug Assertion Failed!

Expand Post »
I have this error when debugging called Debug Assertion Failed , does anyone know what that means? Im using Visual C++ 2008.

C++ Syntax (Toggle Plain Text)
  1. void merge (string alpha, string beta)
  2. {
  3. string temp;
  4. string temp2;
  5. stringstream insert(alpha);
  6. stringstream insert2(beta); // Insert the string into a stream
  7. ostream_iterator<string> output(cout, " ");
  8.  
  9. vector<string> words; //vector to hold words
  10. vector<string> words2;
  11. vector<string> results;
  12.  
  13. while (insert >> temp)
  14. words.push_back(temp);
  15.  
  16. while (insert2 >> temp2)
  17. words2.push_back(temp2);
  18.  
  19. merge(words.begin(), words.end(), words2.begin(), words2.end(), results.begin());
  20.  
  21. cout<<"Merge contains:";
  22. copy(results.begin(), results.end(), output );
  23.  
  24.  
  25.  
  26. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Sep 17th, 2008
1

Re: Debug Assertion Failed!

The error will also tell you what assertion failed and where it failed. Basically it means you used a library class or function the wrong way, such as passing a null pointer to strcpy.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 17th, 2008
0

Re: Debug Assertion Failed!

ok so since it happened after I put in this function its somewhere in here, do you see any problems with this code that would make that error?

C++ Syntax (Toggle Plain Text)
  1. void merge (string alpha, string beta)
  2. {
  3. string temp;
  4. string temp2;
  5. stringstream insert(alpha);
  6. stringstream insert2(beta); // Insert the string into a stream
  7. ostream_iterator<string> output(cout, " ");
  8.  
  9. vector<string> words; //vector to hold words
  10. vector<string> words2;
  11. vector<string> results;
  12.  
  13. while (insert >> temp)
  14. words.push_back(temp);
  15.  
  16. while (insert2 >> temp2)
  17. words2.push_back(temp2);
  18.  
  19. merge(words.begin(), words.end(), words2.begin(), words2.end(), results.begin());
  20.  
  21. cout<<"Merge contains:";
  22. copy(results.begin(), results.end(), output );
  23.  
  24. }
Last edited by JackDurden; Sep 17th, 2008 at 11:39 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Sep 17th, 2008
0

Re: Debug Assertion Failed!

this line is wrong: merge(words.begin(), words.end(), words2.begin(), words2.end(), results.begin());

You call the function with 5 parameters, but the function only expects 2 ( void merge (string alpha, string beta)

What is your program supposed to do?
Last edited by Nick Evan; Sep 17th, 2008 at 11:43 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Sep 17th, 2008
1

Re: Debug Assertion Failed!

>do you see any problems with this code that would make that error?
Yes, and while I'm still miffed that you failed to post the complete text of your error even after I not so subtly informed you that more information is present, I'll tell you what the problem is:
C++ Syntax (Toggle Plain Text)
  1. merge(
  2. words.begin(), words.end(), // OK
  3. words2.begin(), words2.end(), // OK
  4. results.begin()); // Oops! results is empty
You need to use some kind of insert iterator to add new items to an empty vector, or you can resize the vector before calling merge:
C++ Syntax (Toggle Plain Text)
  1. merge (
  2. words.begin(), words.end(),
  3. words2.begin(), words2.end(),
  4. back_inserter ( results ) );
>You call the function with 5 parameters, but the function only expects 2
Can you say "overloading"?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 17th, 2008
1

Re: Debug Assertion Failed!

Click to Expand / Collapse  Quote originally posted by Narue ...
>You call the function with 5 parameters, but the function only expects 2
Can you say "overloading"?
Yes I can. Problem is that I forgot that merge() was a function from the C++ Std lib, hence my reply...
Silly me, disregard my previous post as it is not worth the space somewhere on a webserver...
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Sep 17th, 2008
0

Re: Debug Assertion Failed!

The error looks like this:

Debug Assertion Failed!
line 2508
expression: sequence not ordered

And here is my entire code:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <sstream>
  6. #include <algorithm>
  7. #include <iterator>
  8.  
  9. using namespace std;
  10.  
  11. //prototype of split function
  12. void SplitLines (string& a);
  13. void SplitLines2 (string& b);
  14. void AunionB (string a, string b);
  15. void AinterectB ();
  16. void AdifferenceB ();
  17.  
  18.  
  19.  
  20. int main()
  21. {
  22. string fileName;
  23. string a;
  24. string b;
  25. int names = 0;
  26. ifstream myfile;
  27.  
  28. cout << "Enter file name: ";// Get filename from user
  29. cin >> fileName;
  30.  
  31.  
  32. myfile.open(fileName.c_str());
  33.  
  34.  
  35. while(myfile.good() == false)// Prompt for new file name if not able to open
  36. {
  37. cout << "Unable to open file. Enter a different name: ";
  38. myfile.clear();
  39. cin >> fileName;
  40. myfile.open(fileName.c_str());
  41.  
  42. }
  43.  
  44. while (!myfile.eof())
  45. {
  46. getline(myfile,a, '\n');
  47. getline(myfile,b, '\n');
  48. //function call to split lines into words
  49. SplitLines(a);
  50. cout<<endl;
  51. SplitLines2(b);
  52. cout<<endl;
  53. AunionB (a, b);
  54. }
  55. myfile.close();
  56. return 0;
  57. }
  58. //name of fucntion for splitting
  59. void SplitLines (string& a)
  60. {
  61. string temp;
  62. stringstream insert(a); // Insert the string into a stream
  63.  
  64. vector<string> words; //vector to hold words
  65.  
  66. while (insert >> temp)
  67. words.push_back(temp);
  68. cout<<"Set A: {";
  69. for( int i = 0; i < words.size(); i++ ) {
  70. cout<< words[i] <<","<< " ";
  71. }
  72. cout<<"}";
  73. }
  74. void SplitLines2 (string& b)
  75. {
  76. string temp;
  77. stringstream insert(b); // Insert the string into a stream
  78.  
  79. vector<string> words; //vector to hold words
  80.  
  81. while (insert >> temp)
  82. words.push_back(temp);
  83. cout<<"Set B: {";
  84. for( int i = 0; i < words.size(); i++ ) {//print vector to screen
  85. cout << words[i] <<","<< " ";
  86. }
  87. cout<<"}";
  88. }
  89. void AunionB (string a, string b)
  90. {
  91. string temp;
  92. string temp2;
  93. stringstream insert(a);
  94. stringstream insert2(b); // Insert the string into a stream
  95. ostream_iterator<string> output(cout, " ");
  96.  
  97. vector<string> words; //vector to hold words
  98. vector<string> words2;
  99.  
  100.  
  101. while (insert >> temp)
  102. words.push_back(temp);
  103.  
  104. while (insert2 >> temp2)
  105. words2.push_back(temp2);
  106.  
  107. vector<string> results(words.size() + words2.size());
  108.  
  109. merge(words.begin(), words.end(), words2.begin(), words2.end(), results.begin());
  110.  
  111. cout<<"Merge contains:";
  112. copy(results.begin(), results.end(), output );
  113.  
  114. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Sep 17th, 2008
2

Re: Debug Assertion Failed!

Sort your vectors before merging.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 30th, 2010
0
Re: Debug Assertion Failed!
Reputation Points: 4
Solved Threads: 1
Newbie Poster
shijobaby is offline Offline
14 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Debug Assertion failed. help?
Next Thread in C++ Forum Timeline: Unresolved External Symbol VC++ 2008





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC