-5

I'm kinda new in C++ and when trying to compile this code I get and error that I do not know how to fix:

int main()
{
    typedef pair<int,int> nodo;
    int x;
    cin >> x; 
    int *g;                
    g = new int[x];   

    vector <nodo> g;


    g[1].push_back(nodo(2,5));
    g[1].push_back(nodo(3,10));
    g[3].push_back(nodo(2,12));
    g[2].push_back(nodo(4,1));
    g[4].push_back(nodo(3,2));

    for (int i = 1; i <=4; ++i){
        //    cout << i << " -> ";
        for (int j = 0; j<g[i].size(); ++j){
            //    cout << g[i][j].first << " c: " << g[i][j].second << " ";    
        }
        //   cout << endl;
    }

    dijkstra(1, x);
    system("pause");
    return 0;
}

The error I am receiving is:

Error: Expression must have a class type.
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740

5 Answers5

4

Here:

int *g;
g = new int[x];
vector <nodo> g; // ERROR: Redeclaration!

You are first declaring g to be of type int*, and then you re-declare it to be of type vector<nodo>. This is illegal.

Moreover, you need to have a using namespace std directive if you want to omit the std:: qualification for types in the standard namespace. I do not suggest you using that though. Much better explicitly specifying std::, or rather use specific using declarations.

For instance:

    typedef std::pair<int,int> nodo;
//          ^^^^^
    int x;
    std::cin >> x;
//  ^^^^^
    int *g;
    g = new int[x];

    std::vector <nodo> g;
//  ^^^^^

Also make sure you are importing all the necessary standard headers:

    Type     |  Header
--------------------------
std::vector -> <vector>
std::pair   -> <utility>
std::cin    -> <iostream>
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
1

You're redeclaring g, first it's an int* and then you make it into a vector<int>. I'm not sure how that got past the compiler.

Also, rather than using nodo(1,2) consider using make_pair instead. Using new is also considered bad practice and you should use either a dynamic container like std::vector or a static one like std::array.

Rapptz
  • 20,807
  • 5
  • 72
  • 86
0

pair is not a class because you haven't included <utility>

You also haven't included <vector> or <iostream>.

0

You have two things named g:

int* g;

and

vector <nodo> g;

This wouldn't even compile.

It looks like you want an array of vectors, in which case you need something like

std::vector<std::vector<nodo> > g(x); // size x vector of vectors.

Then you can do this type of thing:

g[1].push_back(nodo(2,5));
g[1].push_back(nodo(3,10));
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

So this version compiles and I think this is what you meant to do:

// Need to include these headers
#include <utility>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    typedef pair<int,int> nodo;
    int x;
    cin >> x; 
    //int *h;                
    //h = new int[x];   

    //specify size of vector
    std::vector< std::vector<nodo> > g(x);

    g[0].push_back(nodo(2,5));
    g[1].push_back(nodo(3,10));
    g[2].push_back(nodo(2,12));
    g[3].push_back(nodo(4,1));
    g[4].push_back(nodo(3,2));


    for (int i = 0; i < g.size(); ++i){
        std::cout << i << " -> ";
        for (int j = 0; j<g[i].size(); ++j){
                cout << g[i][j].first << " c: " << g[i][j].second << " ";    
        }
         cout << endl;
    }

    //dijkstra(1, x);
    //system("pause");
    return 0;
}

Many issues, you use g twice for one. I am not sure what you want to do with the vector but maybe you want a vector of vectors which would be more like this:

 std::vector< std::vector<nodo> > g(x) ;

Then this would make more sense:

 g[0].push_back(nodo(2,5)) ;

The first element of a vector is going to be at 0 not 1.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740