type name not allowed

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
#include<iostream>
#include<cstdio>
#include<list>
#include<vector>
using namespace std;
#define MAX 100001

class Graph
{
public:
	class edge
	{
	public:
		int u,v,weight;
	};
	vector<edge> elist;
	int vertices;

};
int main()
{
	int n,k,x,y,w;
	Graph graph;
	scanf("%d%d",&n,&k);
	for(int i=0;i<n-1;i++)
	{
		scanf("%d%d%d",&x,&y,&w);
		graph.elist.push_back(graph.edge{x,y,w});
	}
}

In the last line "graph.edge{x,y,w}" it says typename is not allowed?
I have used nested class edge and pushing vertices and their weight in elist vector which is of type edge.
Please help!!
class edge has no a constructor with three parameters that you are trying to call in that line.

Oh, I am sorry. You are uisng an initializer list. This construction is invalid.
Last edited on
In the last line "graph.edge{x,y,w}" it says typename is not allowed?

It is referring to the word "edge" after the member access operator . (dot)
Member access operators only allow data members, member functions, and enumerators on the right. Type names are not allowed.

(the standardese for this is "If E2 is a nested type, the expression E1.E2 is ill-formed" in ยง 5.2.5[expr.ref]/4)

Now if you replace that with Graph::edge, which names a type, it will compile
Last edited on
@Cubbi
It is still showing error. Is there any other way? Cant find out how to do this
@vlad
Please suggest some valid construction.
For example you can use this simple approach that should be compiled with old compilers

1
2
3
4
5
6
	for(int i=0;i<n-1;i++)
	{
		scanf("%d%d%d",&x,&y,&w);
		Graph::edge e = { x, y, v };		
		graph.elist.push_back( e );
	}
thats very simple....thanks a lot vlad :)
Topic archived. No new replies allowed.