Help

Can anybody help me? my program doesn't work.
the output message is:
c:\users\shaghayegh\documents\visual studio 2010\projects\test_dijkstra2\network1\network1.cpp(64): error C2228: left of '.push_back' must have class/struct/union
1>
1>Build FAILED.


the program is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

using namespace std;
//using namespace System;

// array::begin example
#include <iostream>
#include <fstream>
#include <array>
#include <vector>
class arc
{
	// const int M=1000000;
public:
	arc(int Id=0, int co=1000000, int tr=1000000) : ID(Id), cost(co), transit_time(tr) {}

	int ID; // The tail(nodes) or head(B) of arc.
	int cost;
	int transit_time;
};

class label
{ public:
const int M=1000000;
label(int pe=1000000, int tem=1000000, int pre=0): permanent(pe), temp(tem), pred(pre){}
int permanent;
int temp;
int pred;
};

class Network{
public:
	Network(int nn=1000000, int ss=1, int tt=1000000, int TT=1000000): n(nn), s(ss), t(tt), T(TT){}
	const int M=1000000;
	const int NumberofNodes=2000;
	std::array<std:: vector<arc>,NumberofNodes+1> nodes; //adjacency List
	std::array<label,NumberofNodes+1> Distance_Label; //To trace Shortest Path
	 int n; //number of nodes.
	 int s; //source
	 int t; // sink
	 int T; //time horizon
	void nodes_initials();
	void create_Network();
	void add_arc_nodes(int , arc );
	void print_shortest_path();
};

void Network::nodes_initials(){
	
//	std::cout << "Please enter the number of nodes:"<< std::endl;
//	std::cin >> n;
	std::cout << "Please enter the ID of source:"<<endl;
	std::cin >> s;
	std::cout << "Please enter the ID of sink:"<<endl;
	std::cin >> t;
	std::cout << "Please enter the time horizon:"<<endl;
	std::cin >> T;
}
void Network::add_arc_nodes(int i, arc a) // i is the head of arc a.
{
	nodes[i].push_back(a);

}
void Network::create_Network() // read the network's parameter  from data file.
{
	int i;//head of arc.
	int j;//tail of arc.
	int co;// cost of arc.
	int tr; //transit time of arc.

	ifstream file;
	file.open("Data.txt", ios::in);
	while(file >> i >> j >> co >> tr)
	{
		arc a(j, co, tr);
		add_arc_nodes(i,a);
	}
	file.close();
}


int main()
{ Network x;
int i;
x.nodes_initials();
x.create_Network();
std::cout<<i;
std::cin>>i;
return 0;
}



just read the error message

left of '.push_back' must have class/struct/union


only push_back is in line 60
left of it is nodes[i]

nodes[] is no where else in the code

=> define nodes[]
I define nodes at line 35; nodes[i] is a vector of class arc.
Last edited on
nodes[] is no where else in the code
it's defined on line 35. So it's supposed to work.

is line 60 the problem?
yes, line 60 is the problem
oops, for some reason i was expecting an array, eventhough it's push_back, sorry my bad.
I don't have compiler that actually supports C++11. My compiler complains that NumberofNodes is not static. Write it like so:

static const int NumberofNodes=2000;

and see if that compiles. Is there any reason why you use std::array?
 Is there any reason why you use std::array?

I just copied
 std::array
from cplusplus.com Reference page.(no reason).
Thank you, I write static const int NumberofNodes=2000;, and delete
const int M=1000000 from classes and it works.

Can't we use const int at class?
don't I need std:: ?
and one more question: If i knew the size of array, and there are no deletion, is preferred to use array or vector?(for memory usage)
Can't we use const int at class?
you can. Without static it's considerd a member variable which cannot be used to create an array

don't I need std:: ?
if you're using namespace std; you don't. I wouldn't recommend it though

If i knew the size of array, and there are no deletion, is preferred to use array or vector?
vector allocates the memory when needed while non dynamic arrays use the memory during their whole lifetime. But todays systems you may consider memory as a (nearly) infinite available resource.

the access to a vector is slower than to a non dynamic array. if time matters...
Thanks so much.
There's one problem: the stack is actually limited. So creating local variables that uses too much memory might burst the stack and crash you program
Last edited on
Topic archived. No new replies allowed.