• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C++ vector question

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Xantom

Member
Joined
Apr 2, 2007
Location
MN
I cannot find what I am doing wrong. From what I have found I am missing an include or a header file, but I can't see it. I have included my .cpp and .h contents. I am using VS.net, thanks in advance.

These are the errors I am getting:
error c2065: 'vector' undeclared identifier
error c2062: type 'int' unexpected

//header
#include <iostream>
#include <tchar.h>
#include <vector>

void printarray (vector<int> *invector);

//.cpp
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#using <mscorlib.dll>
#include <vector>
using namespace std;


void printarray (vector<int> *invector)
{
for (int n=0; n < invector.size(); n++)
cout << invector[n] << " ";
cout << "\n";
}

int main ()
{
vector<int>FA;
FA.push_back(5);
FA.push_back(10);
FA.push_back(15);

vector<int>SA;
FA.push_back(2);
FA.push_back(4);
FA.push_back(6);
FA.push_back(8);
FA.push_back(10);

printarray (&FA);
printarray (&SA);
getch();
return 0;
}
 
Last edited:
I changed the print array method as follows

void printarray (vector<int> *invector)
{
for (int n=0; n < invector->size(); n++)
cout << invector->at(n) << " ";
cout << "\n";
}

I still get the same errors, when I doubles click the error message the cursor takes me to where the method is declared in the .h file.
 

Attachments

  • error.jpg
    error.jpg
    25.8 KB · Views: 236
I was going to say, sounds like a namespace issue.

You could also add "using namespace std;" to the top. -- Paul
 
I cannot find what I am doing wrong. From what I have found I am missing an include or a header file, but I can't see it. I have included my .cpp and .h contents. I am using VS.net, thanks in advance.
The problem seems to be, you are using C++, which is a C compiler with hood scoops and pimpenlights on it. If you're looking for a good object oriented language, consider Objective-C.
 
I changed the print array method as follows

delete line 9, you dont need a function prototype since the printarray function itself is in the file before the main{} function

alternatively move all the headers including "using namespace std;" before the function prototype.
 
Last edited:
The problem seems to be, you are using C++, which is a C compiler with hood scoops and pimpenlights on it. If you're looking for a good object oriented language, consider Objective-C.

Umm, you're crazy. Objective C is used by about .00005% of the number of people using C++. C++ is the standard language, Objective C is hardly used by anyone.
 
Not to mention that it's off-topic. (If this were slashdot, you would be 25% interesting, 25% off-topic, 25% troll, total score: -1.)

Q: I would really like to know how to debug X in language Y.
A: Language Y sucks. Learn language Z.

That's not a proper response. -- Paul
 
Umm, you're crazy. Objective C is used by about .00005% of the number of people using C++. C++ is the standard language, Objective C is hardly used by anyone.

No doubt I'm crazy, I use Gentoo...

The main reason I am disappointed in C++ is, you can write
Code:
 x = 2 + 2;
which, at runtime, results in this being displayed:
Code:
Hello world!

They have tried -- successfully I might add -- everyone's favorite toy features in C++. I merely maintain you should _not_ do that. </religion>

Not to mention that it's off-topic. (If this were slashdot, you would be 25% interesting, 25% off-topic, 25% troll, total score: -1.)
But this isn't slashdot, and I'm not a troll, just someone with strong opinions as to 'Right' programming languages. :D
 
Back