By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

C++ forbids declaration of ‘vector’ with no type

P: n/a
Hi

I need help here. When i compile my program, i get this error:
[juhan@localhost inf2]$ make
g++ -c -Wall inf2_functions.cpp -o inf2_functions.o
inf2_classes.h:6: error: ISO C++ forbids declaration of ‘vector’ with
no type
inf2_classes.h:6: error: expected ‘;’ before ‘<’ token
make: *** [inf2_functions.o] Error 1

My inf2_classes.h looks like that:

1 using namespace std;
2
3 class CookBook {
4
5 private:
6 vector<string> recipes;
7
8 void initRecipes();
9
10 public:
11 CookBook();
12 int getTotal();
13 void showRecipe(int nr);
14 void listRecipes();
15 };
I mean what the hell is that? What ; does it excpect before <???
I didn't have this error before, but when i passed my CookBook object
reference to a function - then this started to happen.

Juhan.
Aug 6 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* Juhan Voolaid:

1 using namespace std;
Don't ever put that in a header file.

2
3 class CookBook {
4
5 private:
6 vector<string> recipes;


'vector' is not a built-in type.
'string' is not a built-in type.
You've forgotten to include the relevant headers.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 6 '05 #2

P: n/a
Have you tried:

vector<std::string> recipes;

instead of

vector<string> recipes;

Is <string> included in your file ?

Dov

Juhan Voolaid wrote:
Hi

I need help here. When i compile my program, i get this error:
[juhan@localhost inf2]$ make
g++ -c -Wall inf2_functions.cpp -o inf2_functions.o
inf2_classes.h:6: error: ISO C++ forbids declaration of 'vector' with
no type
inf2_classes.h:6: error: expected ';' before '<' token
make: *** [inf2_functions.o] Error 1

My inf2_classes.h looks like that:

1 using namespace std;
2
3 class CookBook {
4
5 private:
6 vector<string> recipes;
7
8 void initRecipes();
9
10 public:
11 CookBook();
12 int getTotal();
13 void showRecipe(int nr);
14 void listRecipes();
15 };
I mean what the hell is that? What ; does it excpect before <???
I didn't have this error before, but when i passed my CookBook object
reference to a function - then this started to happen.

Juhan.


Aug 6 '05 #3

P: n/a
Hello again. I think the problem is not about the vector, but about
passing my object reference to a function. Some kind of header conflict
or something maybe.

I have 4 files:
1. main.cpp - main function
2. functions.cpp - other simple functions
3. functions.h
4. cookbook.cpp - a cookbook class
5. cookbook.h

I want to pass CookBook cb object reference to functions located in
functions.cpp, but the CookBook cb object is initialised in main.cpp
function.

My question is - is it possible? I think compiler does not get it. It
gives error that in cookbook.h:
error: ISO C++ forbids declaration of ‘vector’ with no type
error: expected ‘;’ before ‘<’ token
But when i remove the opportunity for doStuff function in functions.cpp
to get access to cb object+functions, it compiles fine.

So i assume, the problem is with giving my cb reference to regular
functions.
Please help.

main.cpp:
/////////
#include <iostream>
#include "cookbook.h"
#include "functions.h"

int main(){
CookBook cb;

doStuff(cb);

return 0;
}

functions.cpp:
/////////////
#include <iostream>
#include "cookbook.h"
#include "functions.h"

void doStuff(CookBook& cb){
cb.doOtherStuff();
}

functions.h:
///////////
void doStuff(CookBook& cb);

cookbook.h:
////////////
class CookBook(){
public:
vector<int> numbers;

CookBook();
void doOtherStuff();
};

cookbook.cpp:
////////////
#include <iostream>
#include <vector>
#include "cookbook.h"

void CookBook::doOtherStuff(){
std::cout<<numbers.size();
}

CookBook::CookBook(){
//initialise cb;
}
Aug 7 '05 #4

P: n/a
* Juhan Voolaid:
Hello again. I think the problem is not about the vector, but about
passing my object reference to a function. Some kind of header conflict
or something maybe.
You're not very good at following advice.

main.cpp:
/////////
#include <iostream>
#include "cookbook.h"
#include "functions.h"

int main(){
CookBook cb;

doStuff(cb);

return 0;
}
OK.
functions.cpp:
/////////////
#include <iostream>
#include "cookbook.h"
#include "functions.h"

void doStuff(CookBook& cb){
cb.doOtherStuff();
}
Technically OK but as a matter of style, to prevent more errors like you
have now, always include standard headers _last_.

functions.h:
///////////
void doStuff(CookBook& cb);

cookbook.h:
////////////
class CookBook(){
Not OK, syntax error.
public:
vector<int> numbers;
Not OK.

You haven't included <vector>.

Microsoft does things the way you do above; competent programmers, on the
other hand, write header files that include all they need.
CookBook();
void doOtherStuff();
};

cookbook.cpp:
////////////
#include <iostream>
#include <vector>
Not OK, do not ever include headers that are needed by other of your
headers; modify your headers to themselves include all they need.

#include "cookbook.h"

void CookBook::doOtherStuff(){
std::cout<<numbers.size();
}

CookBook::CookBook(){
//initialise cb;
}


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 7 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.