tags:

views:

102

answers:

2

I want to create a list of queues in C++ but the compiler gives me some cryptic messages:

#include <list>
#include <queue>

class Test
{
    [...]
    list<queue> list_queue;
    [...]
}

Output:

error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'

It gives me the same error even if I use int as the template paramenter. What's going on?

(btw, I'm using VC++ 2008 EE)

+8  A: 

queue is a template class as well, so you'll need to specify the element type contained in your queues. Also, - is not a legal identifier character in C++; perhaps you meant _?

std::list<std::queue<SOME_TYPE_HERE> > list_queue;
280Z28
beat me by 17 seconds :)
San Jacinto
Yeah, replaced `-` by `_`. Stupid Lisp :P.
MeDiCS
@MeDiCS: I figured it was lisp :D
280Z28
where do you see a "-" (hyphen or dash) in an identifier? I see it only in the error messages.
Ben Voigt
@Ben Voigt: in my original, unedited post.
MeDiCS
hmm, the question says "asked (time) ago", not "edited". So I thought what I'm seeing is the question as originally asked.
Ben Voigt
@Ben Voigt: When the same author does a ninja edit sometimes it doesn't show in the history.
280Z28
+1  A: 

also "using namespace std", and there needs to be a semicolon after your class definition

280Z28 is right that "using" in a header file is a bad idea for production code. It's still a reasonable troubleshooting step though, to quickly see if the primary problem is identifier search scope.

Ben Voigt
I always forget this ^^'
MeDiCS
-0 for `using namespace std;` - should be `std::list` and `std::queue` instead.
280Z28
Why? It's not like we'll be getting a lot of conflicts (plus, I hate typing)
MeDiCS
@MeDiCS: If you hate typing, programming isn't for you. You don't program to save keystrokes anymore than you swim to stay dry. `using namespace std` is generally bad, *especially* in a header file.
GMan
@GMan: (error: could not detect sarcasm). Actually, I was asking why not use it.
MeDiCS
@MeDiCS: In C/C++, you never know which files are going to end up as parts of other files. When you add `using namespace std;`, it can effectively "rearrange" the type hierarchy as seen by other files, and it's a nightmare to keep track of. `using namespace` and `#include` cannot peacefully coexist, and since `#include` isn't going anywhere ....
280Z28
@GMan: I see. Thanks!
MeDiCS
If you really want to avoid `std::` you can use a using-declaration `using std::list;` which lets you use `std::list` as `list` in the current scope. http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5
Philip Potter