Sign in to follow this  
becklighter

adding a queue to a class

Recommended Posts

becklighter    138
I'm attempting to add a queue as a private member of my class, but get the error " syntax error : missing ';' before '<' " on the line where it is declared, I would appreciate any insight into why I'm unable to compile.

here is an example of my code:
[source]
#include <queue>

class GameBoard{
private:
queue<int> upcomingPieces;
};
[/source] Edited by becklighter

Share this post


Link to post
Share on other sites
NumberXaero    2624
[quote name='becklighter' timestamp='1313360304' post='4849142']
I'm attempting to add a queue as a private member of my class, but get the error " syntax error : missing ';' before '<' " on the line where it is declared, I would appreciate any insight into why I'm unable to compile.

here is an example of my code:
[source]
#include <queue>

class GameBoard{
private:
queue<int> upcomingPieces;
}
[/source]
[/quote]

[code]
#include <queue>

class GameBoard{
private:
queue<int> upcomingPieces;
}; // <---
[/code]

Share this post


Link to post
Share on other sites
becklighter    138
[quote name='NumberXaero' timestamp='1313360569' post='4849144']
[quote name='becklighter' timestamp='1313360304' post='4849142']
I'm attempting to add a queue as a private member of my class, but get the error " syntax error : missing ';' before '<' " on the line where it is declared, I would appreciate any insight into why I'm unable to compile.

here is an example of my code:
[source]
#include <queue>

class GameBoard{
private:
queue<int> upcomingPieces;
}
[/source]
[/quote]

[code]
#include <queue>

class GameBoard{
private:
queue<int> upcomingPieces;
}; // <---
[/code]
[/quote]

Thanks for pointing out that my class was missing the ";", but that isn't the problem. The code in my project actually had that part right, I just didn't copy it all over, any other ideas?

Share this post


Link to post
Share on other sites
DoctorGlow    1808
[quote name='NumberXaero' timestamp='1313360569' post='4849144']
[quote name='becklighter' timestamp='1313360304' post='4849142']
I'm attempting to add a queue as a private member of my class, but get the error " syntax error : missing ';' before '<' " on the line where it is declared, I would appreciate any insight into why I'm unable to compile.

here is an example of my code:
[source]
#include <queue>

class GameBoard{
private:
queue<int> upcomingPieces;
}
[/source]
[/quote]

[code]
#include <queue>

class GameBoard{
private:
queue<int> upcomingPieces;
}; // <---
[/code]
[/quote]

and

[code]
#include <queue>

class GameBoard{
private:
std::queue<int> upcomingPieces; // <-- std::
};
[/code]

Share this post


Link to post
Share on other sites
Adaline    710
Hello :)

There are 2 options :
[list][*]use [b]std::queue[/b] instead of [b]queue[/b] everywhere[b]
[/b][*]or [s]put [b]using namespace std;[/b] after the[b] #include<queue>[/b][/s] (sorry :()[b]
[/b][/list]

Share this post


Link to post
Share on other sites
Adaline    710
[quote name='rip-off' timestamp='1313394843' post='4849284']
....as there is no way to "un-use" a namespace.
[/quote]

Why would you 'un-use' a namespace ?
std is used with vector, string,list,etc.... so putting std:: everywhere is not very elegant

Share this post


Link to post
Share on other sites
ApochPiQ    23064
[quote name='Adaline' timestamp='1313396366' post='4849289']
[quote name='rip-off' timestamp='1313394843' post='4849284']
....as there is no way to "un-use" a namespace.
[/quote]

Why would you 'un-use' a namespace ?
std is used with vector, string,list,etc.... so putting std:: everywhere is not very elegant
[/quote]


"using" a namespace in a header is bad.

What if I want to call something foo::string? If I #include your header, I now have a name collision between foo::string and std::string.

You should only be "using" namespaces from within .cpp files, preferably [i]after[/i] all headers are included, and then only as absolutely necessary.

Share this post


Link to post
Share on other sites
Adaline    710
What about inline classes ? Where else could you put the using directive ? Isn't it a bad idea to call a class 'string' anyway ?

Share this post


Link to post
Share on other sites
Hodgman    51336
[quote name='Adaline' timestamp='1313396366' post='4849289']std is used with vector, string,list,etc.... so putting std:: everywhere is not very elegant[/quote]A standard C++ [url="http://stackoverflow.com/questions/5849457/using-namespace-in-c-headers"]r[/url][url="http://stackoverflow.com/questions/6077566/using-namespace-std"]u[/url][url="http://stackoverflow.com/questions/4872373/why-is-including-using-namespace-into-a-header-file-a-bad-idea-in-c"]l[/url][url="http://nepsweb.co.uk/pgtcpp/namespace.htm"]e[/url], that's in almost every C++ style guide I've ever read is: [b]Never use a "using namespace" statement globally in a header[/b].

It pollutes the global namespace in the same way that #defines do, which is something that namespaces were designed to avoid.
e.g. this is the problem with defines:[code]#include "windows.h"
#include <algorithm>
int a = 2, b = 4;
int c = std::max( a, b );//compile error - windows contains a #define max line!!![/code]

Now, namespaces solve these kinds of problems for us.
Here is some perfectly valid code:[code]#include <queue>
class queue{};
queue q;//makes my queue, not a std::queue
std::queue<int> q2;//makes a std::queue[/code]Now, if we include a header that has rudely used "using namespace" inside it:[code]#include "Adaline.h" // includes "using namespace std;"
#include <queue>
class queue{};
queue q; // compile error? This code was fine a minute ago?!! thanks Adaline ;)
std::queue<int> q2;[/code]

If you really do want to use "using namespace" in a header, then you must at least always wrap it in your own namespace, e.g.[code]//Adaline.h
#include <queue>
namespace adaline
{
using namespace std;//ok, you've only polluted your own namespace
}[/code]
[quote name='Adaline' timestamp='1313396893' post='4849291']Isn't it a bad idea to call a class 'string' anyway ?[/quote]The concept extends beyond the [font="Courier New"]std[/font] namespace.
Maybe I'm using two different bits of C++ middleware that both define a Vector3 type? If I using-namespace'd them, I'd get conflicts between the two libraries.

Share this post


Link to post
Share on other sites
Adaline    710
[quote name='Hodgman' timestamp='1313396905' post='4849292']
[quote name='Adaline' timestamp='1313396366' post='4849289']std is used with vector, string,list,etc.... so putting std:: everywhere is not very elegant[/quote]A standard C++ [url="http://stackoverflow.com/questions/5849457/using-namespace-in-c-headers"]r[/url][url="http://stackoverflow.com/questions/6077566/using-namespace-std"]u[/url][url="http://stackoverflow.com/questions/4872373/why-is-including-using-namespace-into-a-header-file-a-bad-idea-in-c"]l[/url][url="http://nepsweb.co.uk/pgtcpp/namespace.htm"]e[/url], that's in almost every C++ style guide I've ever read is: [b]Never use a "using namespace" statement globally in a header[/b].

It pollutes the global namespace in the same way that #defines do, which is something that namespaces were designed to avoid.
e.g. this is the problem with defines:[code]#include "windows.h"
#include <algorithm>
int a = 2, b = 4;
int c = std::max( a, b );//compile error - windows contains a #define max line!!![/code]

Now, namespaces solve these kinds of problems for us.
Here is some perfectly valid code:[code]#include <queue>
class queue{};
queue q;//makes my queue, not a std::queue
std::queue<int> q2;//makes a std::queue[/code]Now, if we include a header that has rudely used "using namespace" inside it:[code]#include "Adaline.h" // includes "using namespace std;"
#include <queue>
class queue{};
queue q; // compile error? This code was fine a minute ago?!! thanks Adaline!
std::queue<int> q2;[/code]

If you really do want to use "using namespace" in a header, then you must at least always wrap it in your own namespace, e.g.[code]//Adaline.h
#include <queue>
namespace adaline
{
using namespace std;//ok, you've only polluted your own namespace
};[/code]
[/quote]
Thanks for you're explanation (I mean it)
No need to be rude anyway

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this