'list' is used as a type, but is not defined as a type.

I created a class called Vertices and one of it's data members is a list of integers representing which Vertices that specific vertex points to.

Here is the code:

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
/*
  Vertices.h

  Created by Matthew Dunson on May 13 2010

*/

#ifndef VERTICES_H
#define VERTICES_H

#include <list>

class Vertices
{
private:
    bool mark;
    int fromVertex;
    list<int> toVertices;
    int parent;
    int discover_time;
    int finish_time;

public:
    Vertices (int vertexNumber);
    bool getMark();
    int getFromVertex ();
    list<int> getToVertices ();
    int getParent ();
    int getDiscoverTime ();
    int getFinishTime ();
    void setMark (bool isVisited);
    void setFromVertex (int theVertex);
    void addVertex (int toVertex);
    void setParent (int parentVertex);
    void setDiscoverTime (int timeDiscovered);
    void setFinishTime (int timeFinished);
};

#endif 


I tried to compile this class and got the following error:


% In file included from detect_cycle.cpp:5:
Vertices.h:18: 'list' is used as a type, but is not defined as a type.
Vertices.h:27: parse error before `)' token


What am i doing wrong? I included the list library so why is there a problem?
std::list<> or use - using namespace std
Thank you!
Thank you!
No problem.
In header fines you should seriously avoid doing a 'using namespace std;'

It should only really be used in the .cpp files.
Yes.

Never use using statements in headers.
You do not have the right to pollute other people's namespaces just because they #included your header.

Instead, make sure to fully qualify the name, as Mythios first suggested.

18
19
    std::list<int> toVertices;
    ...
27
28
    std::list<int> getToVertices ();
    ...

Hope this helps.
I didn't realize it was bad to use using in headers. I myself have never even used using before because from day one of starting c++, I was told - "Never use using namespace std" from my tutor :D
I like your tutor.
He was a great person, great tutor and has so much knowledge. Sadly the few game titles his worked on weren't shipped. But he does have a few under his belt. But he was working on a Doctor who game, which died at the end sadly.
Last edited on
Sorry to hear that.

About using... always/never rules are circumspect. To say "never use using namespace std" is not good advice. (Heck, there may even be times when using 'using' in a header might be correct.)
Curious, as I personally don't know. What would be a correct way of using it in a header?
If you used it in a function body such as an inline function or a template. Then it would not pollute the global namespace.
Also, sometimes the point of including a header is to include a whole bunch of stuff and initialize namespaces. Rare, but true.
Topic archived. No new replies allowed.