Perhaps it is a header problem of sorts... But this is what's happening:
The compiler is giving me an error on the line:
Queue<Email> mailbox;
This is the error:
..\EmailSystem.h:25: error: ISO C++ forbids declaration of `Queue' with no type
..\EmailSystem.h:25: error: expected `;' before '<' token
Queue.h:
#ifndef QUEUE_H_
#define QUEUE_H_
#include <string>
#include "EmailSystem.h"
...
template <class B>
class Queue {
...
};
#endif /* QUEUE_H_ */
Queue.cpp:
#include "Queue.h"
...
template class Queue<Email>;
EmailSystem.h:
#ifndef EMAILSYSTEM_H_
#define EMAILSYSTEM_H_
#include <iostream>
#include <string>
#include <vector>
#include "Queue.h"
struct Email {
...
};
struct User {
std::string name;
Queue<Email> mailbox;
};
...
#endif /* EMAILSYSTEM_H_ */
You have a circular include. Queue.h includes EmailSystem.h and EmailSystem.h includes Queue.h so the include guards make sure that the header has no effect the second time it is being included. This means if Queue.h is the first to be included then Queue will not yet be declared before it is first used in EmailSystem.h which it includes, at this point:
Queue<Email> mailbox;
I'm guessing, but I find it unlikely that your template Queue (if it really is a generic class template) needs to know about Email so you should probably remove #include "EmailSystem.h" from Queue.h to solve your issue.
You #include "EmailSystem.h" in Queue.h, before you declare class Queue. So when the compiler tries to figure out how to create struct User, it has no clue what's the Queue<Email> you're trying to use.
Note that EmailSystem.h and Queue.h include each other.
Related
I am attempting to create a templated vector class, but upon compilation I am receiving an error of
def.hpp:3:1: error: 'TempVector' does not name a type
I keep referring to reference material and my syntax and handling of the header file declaration and definition (.h and .hpp) seem right to me, but I can not figure out what I am overlooking.
Below is the three files I am working with, thank you.
driver.cpp:
#include <iostream>
#include <string>
#include "dec.h"
using namespace std;
int main() {
TempVector <int> v1;
cout<<"ran successfully"<<endl;
}
dec.h:
#ifndef DEC_H
#define DEC_H
#include <iostream>
#include <utility>
// Declaration of class Vector
template <typename T>
class TempVector {
public:
TempVector ();
private:
T* array;
static const unsigned int spare = 10;
};
#include "def.hpp"
#endif
def.hpp:
template <typename T>
TempVector<T>::TempVector () {
std::cout<<"ran successfully";
}
I recently started learning C++.
I would like to know why it is not possible to define a variable in a header file like this :
#ifndef DUMMY_H
#define DUMMY_H
class Dummy
{
stack<std::pair<int, int>> s;
};
#endif //DUMMY_H
You are missing:
a #include <stack> statement, so the compiler knows what stack is (and a #include <utility> statement for std::pair).
a using namespace std; or using std::stack; statement, so you can use std::stack without specifying the std:: prefix.
Try this:
#ifndef DUMMY_H
#define DUMMY_H
#include <stack>
#include <utility>
using std::stack;
class Dummy
{
stack<std::pair<int, int>> s;
};
#endif //DUMMY_H
You really shouldn't use a using statement in a header file *, unless it is nested inside of an explicit namespace:
#ifndef DUMMY_H
#define DUMMY_H
#include <stack>
#include <utility>
class Dummy
{
std::stack<std::pair<int, int>> s;
};
#endif //DUMMY_H
* using a type/namespace into the global namespace can cause undesirable side effects if you are not careful!
You must include required header before using them.
Also precaution has to be taken care to add appropriate namespace resolution.
#ifndef DUMMY_H
#define DUMMY_H
#include <stack>
#include <utility> // This has added for pair
class Dummy
{
std::stack<std::pair<int, int> > s; // Notice the space between > >.
};
#endif //DUMMY_H
Additional space is required in earlier version of C++98 for grammatical reason.
More information: Template within template: why "`>>' should be `> >' within a nested template argument list"
This is not required from C++03
I have been having a lot of trouble with my headers and making sure everything is declared correctly. First off my files:
//Main.cpp
#include "Item.h"
#include "Warehouse.h"
using namespace std;
int main() {
...
}
//Item.h
#ifndef ITEM_H
#define ITEM_H
#include <string>
using namespace std;
class Item {
...
};
#endif /* ITEM_H */
//Item.cpp
#include "Item.h"
//Warehouse.h
#define WAREHOUSE_H
#ifndef ITEM_H
#define ITEM_H
using namespace std;
class Item;
class Warehouse {
...
private:
Item* array; //problem starts with this
};
#endif /* WAREHOUSE_H */
//Warehouse.cpp
#include "Warehouse.h"
#include "Item.h"
Warehouse::Warehouse() {
array = new Item[arraySize]; //and this is where I get the error
}
I am pretty sure the problem has to do with my header in Warehouse.h but every combination I try does not work. Sorry if not enough of the code is posted but I figure the problem is with the includes and declarations.
Thanks ahead of time.
edit: to clarify this is not in one file. I just wrote it like this to simplify things. Each one of the above is a separate file.
Your include guards in the header file Warehouse.h are not correct.
Instead of
//Warehouse.h
#define WAREHOUSE_H
#ifndef ITEM_H
#define ITEM_H
using namespace std;
// ...
#endif /* WAREHOUSE_H */
you want
//Warehouse.h
#ifndef WAREHOUSE_H
#define WAREHOUSE_H
using namespace std;
// ...
#endif /* WAREHOUSE_H */
With the current version the class definition in item.h is never included in Warehouse.cpp because the mixed-up include guards in Warehouse.h prevent item.h to be read due to the order of
//Warehouse.cpp
#include "Warehouse.h"
#include "Item.h" //Warehouse.cpp
#include "Warehouse.h"
#include "Item.h"
Then the compiler does not know the definition of Item at that point, hence the error.
Another thing: Do not form the habit of using namespace std in header files. This will lead to issues at some point.
Problem is not in this declaration
private:
Item* array; //problem starts with this
You may define a pointer to an incomplete type.
I think the problem is in a statement where you try to allocate an object for this pointer using operator new or to dereference the pointer.
Also I do not see any reason why you do not want to include header Item.h in header Warehouse.h instead of using elaborated name
class Item;
This question already has answers here:
Can standard container templates be instantiated with incomplete types?
(3 answers)
Closed 8 years ago.
I defined two header files.
global.h
#ifndef GLOBAL_H
#define GLOBAL_H
#include <queue>
#include <string>
//#include "token.h"
class Token;
typedef std::string TokenValue;
enum TokenType{//...};
inline void clear(std::queue<Token> tokens)
{
std::queue<Token> empty;
std::swap(tokens, empty);
}
#endif // GLOBAL_H
and token.h
#ifndef TOKEN_H
#define TOKEN_H
#include "global.h"
class Token
{
public:
Token (TokenType token_type, TokenValue token_value)
{
token_type_ = token_type;
token_value_ = token_value;
}
~Token (){}
//...
private:
TokenType token_type_;
TokenValue token_value_;
};
I use foward declaration in global.h, but i don't use the reference or pointer of class Token. I use std::queue<Token> empty; in global.h. I think this statement must need the size of Token. I can't figure out why it can compiles success. It's the problem of queue?
When you include global.hin token.h the compiler has the complete information to work. Try to include global.h in another file and you will have your compile error :-)
I am getting errors with the following code. The errors are incomplete type is not allowed and use of undefined type 'mGame'.
header.h:
//--Libraries
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//--Classes
class mGame;
Game.cc:
#include "header.h"
class mGame
{
private:
public:
bool intro();
};
Intro.cc:
#include "header.h"
bool mGame::intro() //--Line 3
{
printf("|-----------------------------|\n");
printf("\n Welcome to the Guessing Game!\n");
printf("\n|-----------------------------|\n");
return false;
}
The errors are both on line 3 of intro.cc. I tried finding a solution, but I couldn't for what I am doing.
header.h doesn't know any definitions of game.cc, you tell header.h only, that there is a class mGame. rename game.cc to game.h and include it into header.h and delete the line "class mGame;"
To be able to use mGame from Intro.cc, you have to move the class declaration into header.h (or into some other header file that you include from Intro.cc).
Having a forward declaration in header.h is not enough (that's what is meant by "incomplete type is not allowed").