2

Just started to learn vector, but I got this error from VC++2010 and stuck there, help please....

"error C2228: left of '.push_back' must have class/struct/union"

#include <vector>
#include <iostream>
#include <limits>

using namespace std;

int main()
{
    vector<double> myVector();
    double temp = 30.0;
    myVector.push_back(temp);   //this line makes error ?? why??

    return 0;
}
liau
  • 23
  • 1
  • 5

3 Answers3

7
vector<double> myVector();

this line looks like a forward declaration of a function returning vector.

It should be just

vector<double> myVector;
Karadur
  • 1,226
  • 9
  • 16
2

Your problem is here, you are declaring a function not a vector, this is known as most vexing parse. What you currently have is being interpreted as a forward declaration of a function that takes no arguments that returns a vector<double>:

vector<double> myVector();
                       ^^

if you just want to use the default constructor, it needs to be:

vector<double> myVector ;

Unrelated to your current issue, I would advise against using namespace std;. I realize it is everywhere and it saves some typing but in the long run it will cause you problems.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
2

This sentence is wrong:

vector<double> myVector();

or the complier will consider the myVector a function which return a value of vector!!

you should define a vector like this:

vector<double> myVector
minicaptain
  • 1,196
  • 9
  • 16