Advertisement

Trouble with STL-vectors

Started by April 30, 2001 08:43 AM
2 comments, last by OpenGLCoder 23 years, 9 months ago
Hello Can someone help me fixing my problems using the Standard Template Library ? I am using a vector-template in the following way : #include . . . vector vec(3); All running under Visual Studio 6.0 I got a lot of errors like this ( sorry for the german error output, but if you know where''s the problem ) e:\programme\nt\microsoft visual studio\vc98\include\new(35) : error C2061: Syntaxfehler : Bezeichner ''THIS_FILE'' e:\programme\nt\microsoft visual studio\vc98\include\new(35) : error C2091: Funktionsergebnis ist eine Funktion Any idea ??? Also #include didn''t work...

Not sure what those German errors say, but..

(1) did you include a line in your file "using namespace std;" ? Otherwise you need to declare your vector like "std::vector"

(2) To declare an array, use [3] and not (3).

// CHRIS
// CHRIS [win32mfc]
Advertisement
First, yes, you are correct. You need to add a line in your program that says "using namespace std;".

Second, he''s not declaring an array, he''s declaring a vector, which is declared like, "vector v(size, value);". So if you want to declare a vector of 10 things, you would say, "vector v(10);", and if you want to declare a vector of 20 things, and set all of those things equal to 1, then you would say, "vector v(20,1);". A vector works much like an array (only with a lot more functionality). Vector actually uses an array under the covers, but it adds a lot of functionality like resizing and stuff like that. And all of the memory allocation/deallocation is all done under the covers. It''s very nice. I had to re-write the STL''s version of the vector template class, so I know a little bit about it
The syntax errors are from new. It looks like you''re using an MFC boilerplate file that has this code in just about every .cpp file:
  #ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif  


For some reason, it looks like this isn''t working correctly. Actually, guessing from the code, I''d say the debug version of new is being called, but this block above was accidentally erased, so the debug new function call can''t find "THIS_FILE".

Here is a piece of code you should be able to slap into a new project to make sure that STL is working properly:
  // somefile.cpp#include <vector>using namespace std;int main (){  vector<int> testVec;  testVec.resize (4);  testVec.push_back (3);  return testVec.back (); // should return 3}  


If that doesn''t compile and run, you have a problem with STL. If it does, you have a problem with your other program integrating with STL.

Make sure you have the latest service pack installed when using STL with MSVC--it won''t work without at least service pack 3.

This topic is closed to new replies.

Advertisement