Vector error in Visual

G

Graeme

I just recently obtained a copy of Visual C++ in order to make it
easier for a friend and I to colaborate on a game. Previously, I had
been using Dev-C++.

The problem I have is that the compiler fails to recognize the vector
class. I #include <vector>, but it still has problems recognizing the
type. I have heard that including *.h files can really screw up the
compiler in this way, and I am using <stdlib.h> and <stdio.h>. I also
found out that the vector class needs the 'using std::vector'
declaration (even though string seems to work fine), but when I add
this to the code, it has problems with the following function, calling
object an undeclared identifier:

template<class C>
void INV_ReleaseContainer(C& object)
{
C temp;
temp.swap(object);
}

My friend, an experienced user of Visual C++, has no idea what is
going on, and the code compiled just fine in Dev. I have the feeling
that several things are working against me at once, so I'm not really
sure what to try next. Any help would be greatly appreciated!
Thanks!

Graeme
 
W

White Wolf

Graeme said:
I just recently obtained a copy of Visual C++ in order to make it
easier for a friend and I to colaborate on a game. Previously, I had
been using Dev-C++.

The problem I have is that the compiler fails to recognize the vector
class. I #include <vector>, but it still has problems recognizing the
type. I have heard that including *.h files can really screw up the
compiler in this way, and I am using <stdlib.h> and <stdio.h>. I also
found out that the vector class needs the 'using std::vector'
declaration (even though string seems to work fine), but when I add
this to the code, it has problems with the following function, calling
object an undeclared identifier:

template<class C>
void INV_ReleaseContainer(C& object)
{
C temp;
temp.swap(object);
}

I assume that you are using here a vector as template argument???

Please (as usual) post the shortest possible compilable code which still has
the error. Then we will have a chance to see what is going on.
 
M

Mike Wahler

Graeme said:
I just recently obtained a copy of Visual C++ in order to make it
easier for a friend and I to colaborate on a game. Previously, I had
been using Dev-C++.

The problem I have is that the compiler fails to recognize the vector
class. I #include <vector>, but it still has problems recognizing the
type. I have heard that including *.h

None of the standard C++ headers have '.h' in
their names. For #including files such as your
own headers, the file name is irrelevant, and
need only comply with naming rules of your
OS and/or implementation.

files can really screw up the
compiler

I've never seen an #include directive 'screw up' a compiler.
in this way,

In what way?
and I am using <stdlib.h> and <stdio.h>.

Those are valid (but deprecated) standard headers.
Note: beware mixing the C stdio and C++ iostream
operations, which can give unexpected results.
I also
found out that the vector class needs the 'using std::vector'
declaration

Yes, all standard C++ library identifiers (except
those of macros) are declared in namespace 'std'.
(even though string seems to work fine),

Blind acceptance of "it seems to work" is imo probably
one of the most prolific generators of inexplicable or
abberant behavior of software. Especially inconsistent
behavior. Learn the language well, and then you'll *know*
if your code is correct or not. If you *know* your code is
correct, and how it's supposed to behave, then when it doesn't,
you have excuse to point your finger at the compiler.
Otherwise not.
but when I add
this to the code, it has problems with the following function, calling
object an undeclared identifier:

template<class C>
void INV_ReleaseContainer(C& object)
{
C temp;
temp.swap(object);
}
The function looks OK to me. It should compile just
fine in a file by itself with no embellishment or change.
We'll need more context. Try to create a small, compilable
program that gives the same error and post it here.
My friend, an experienced user of Visual C++, has no idea what is
going on, and the code compiled just fine in Dev. I have the feeling
that several things are working against me at once, so I'm not really
sure what to try next. Any help would be greatly appreciated!
Thanks!

The following is adapted from your code and description,
compiles successfully with MSVC++ v6.0 (SP5), and gives
expected output:

#include <iostream>
#include <vector>
template<class C>

void INV_ReleaseContainer(C& object)
{
C temp;
temp.swap(object);
}

int main()
{
std::vector<int> vec;
for(int i = 0; i < 5; ++i)
vec.push_back(i);

std::cout << "vector contains " << vec.size() << " elements.\n";
INV_ReleaseContainer(vec);
std::cout << "vector contains " << vec.size() << " elements.\n";

return 0;
}


Output:

vector contains 5 elements.
vector contains 0 elements.


HTH,
-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top