-1

I have this code that in my mind, it recieved an item called Vehicle and it has to store it in an array called Node. This is the code related to this part of the program:

void Table::process(Vehicle v, int cont) {
    char a='A'+cont;
    putVehicle(a,v);
    Node.a_v[cont]=v;
    if(cont==0) a_surt=v.rowVehicle();
}

This is how I have the array on the private part of Table.h:

struct Node{
    Vehicle a_v;
};

The error I get is:

error: expected primary-expression before '.' token

I have the includes I need, but everytime I type this: Node.a_v It gives me that error. Any advice?

p. bosch
  • 139
  • 2
  • 10
  • 4
    `Node` is not an array, it's not even a variable, it's a type. You can *use* a type to *declare* an array or a variable, but in itself a type is not an array or variable. You need to get these concepts straight. – john Apr 26 '13 at 13:33

4 Answers4

4

If you want to use a struct, you need to declare a Node before using it. Also, the struct needs to contain an array (or better, look into vectors for more flexibility).

struct Node {
    Vehicle[10] a_v; // 10 is max number of Vehicles in array
};

Node myNode;
myNode.a_v[cont] = v;

Remember that if you want to keep this Node around and put more things in it, it needs to be declared in the right scope. For example, to have your process function add a Vehicle to a Node that exists outside of the function process, you could something like this:

void Table::process(Node n, Vehicle v, int cont) {
    char a = 'A'+cont;
    putVehicle(a,v);
    if (cont < 10) {
        n.a_v[cont] = v;
    }
    if (cont == 0) a_surt = v.rowVehicle();
}

It kind of looks like you're just trying to use an array. In that case you're looking for something like this:

// This would go somewhere in your program. Again, 10 is just an example.
Vehicle vehicleArray[10];

// Send this array to this function
void Table::process(Vehicle[] vArray, Vehicle v, int cont) {
    char a = 'A'+cont;
    putVehicle(a,v);
    if (cont < 10) { // In a real program, don't hard-code array limits.
        vArray[cont] = v;
    }
    if (cont == 0) a_surt = v.rowVehicle();
}
Victor Sand
  • 2,270
  • 1
  • 14
  • 32
3

You should use Node object to get access to the a_v variable. This line

Node.a_v[cont]=v;

Is incorrect. You should do something like that:

Node n;
n.a_v[cont]=v;
awesoon
  • 32,469
  • 11
  • 74
  • 99
2

everytime I type this: Node.a_v It gives me that error.

Node is a type; types define the structure of a objects, but they do not have fields of their own (except the static fields, which belong to all instances at once; they are accessed differently anyway).

In order to use a . or -> operator, you need an instance of a Node, like this:

Node x;
x.a_v = ...

It is not clear in your case from where the Node instances should be coming, though. In order to access them, you would need to either pass them in as parameters, or make them available statically/globally (not recommended).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Okay, so Node is NOT the name of your array. It's the name of a user-defined type that is supposed to contain an array. Your Node, however, does not contain an array. It contains one Vehicle, named a_v. I assume a_v is supposed to represent an Array of Vehicles. Therefore, you need to allocate the array. Something like this:

struct Node {
   Vehicle a_v[AMOUNT];
};

If you don't know at compile-time how large you want your arrays to be, then they must be dynamically allocated, like this:

struct Node {
   Vehicle* a_v;
   Node() {
      a_v = new Vehicle[AMOUNT];
   }
};

If it's dynamically allocated, then it must also be deallocated:

struct Node {
   Vehicle* a_v;
   Node() {
      a_v = new Vehicle[AMOUNT];
   }
   ~Node() {
      delete[] a_v;
   }
};

AND if it's dynamically allocated, you need to add provisions for copying or disable copying:

struct Node {
   Vehicle* a_v;
   Node() {
      a_v = new Vehicle[AMOUNT];
   }
   ~Node() {
      delete[] a_v;
   }

   // Disable copies (with C++11 support):
   Node(const Node&) = delete;
   Node& operator=(const Node&) = delete;

   // Disable copies (without C++11 support) by making them private and not defining them.
   private:
   Node(const Node&);
   Node& operator=(const Node&);
};

Then to access one of the Vehicles, you'd need to do so like this:

Node n;  // Declare a node, which contains an array of Vehicles
n.a_v[cont] = v;  // Copy a Vehicle into the array of Vehicles

Note, however, that if you declare the Node instance in this function, then it is local and it will go out of scope as soon as your function ends. You need to declare the Node instance as a member of your Table if you want it to persist past the function call.

class Table
{
   private:
      Node n;
};

Lastly, as others have suggested, I'd highly recommend that you read a C++ book to learn C++. My personal recommendation is this book (5th edition, don't buy 6th or 7th - the author of those editions is terrible).

derpface
  • 1,611
  • 1
  • 10
  • 20