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
Related
I'm being tasked with defining a struct within my main() function, but using it in other files. I have the code working if I define my struct inside my header file, but I cannot figure out how to define struct inside main() and still use it outside its given scope.
Example of what I have now:
3 files: main.cpp, header.h, and function.cpp
main.cpp
#include <iostream>
#include "header.h"
using namespace std;
int main()
{
vector<myStruct> myVec;
myFunction(myVec);
return 0;
}
header.h
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <vector>
using namespace std;
struct myStruct{
int typeInt;
string typeString;
double typeDouble;
};
void myFunction(vector<myStruct>&);
#endif // HEADER_H_INCLUDED
function.cpp
#include <iostream>
#include <vector>
#include "header.h"
using namespace std;
void myFunction(vector<myStruct>& myVec){
myVec.push_back(myStruct());
myVec[0].typeInt=5;
cout<<myVec[0].typeInt<<endl;
}
Right now, this works for what I need it to do. Unfortunately, I'm told I cannot define struct myStruct inside header.h but instead must have it within main() in main.cpp.
I've tried changing my code to the following (function.cpp unchanged):
main.cpp v2
#include <iostream>
#include "header.h"
using namespace std;
int main()
{
struct myStruct{
int typeInt;
string typeString;
double typeDouble;
};
vector<myStruct> myVec;
myFunction(myVec);
return 0;
}
header.h v2
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <vector>
using namespace std;
template <typename myStruct>
void myFunction(vector<myStruct>&);
#endif // HEADER_H_INCLUDED
Now I receive the error:
error: 'myStruct' was not declared in this scope on line 7 of function.cpp.
How can I use myStruct in function.cpp, while still defining myStruct in main() of main.cpp?
this way using template is correct, that problem occur in function.cpp right?
you try to define the function "myFunction(..)" in header.h,
and remove function.cpp, this program will work well.
if you must implement that fuction in function.cpp, you have to create a template class.
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;
I am wondering why I am getting error "string could not be resolved to type" when I have the proper inclusions?
#ifndef EVENTFILEREADER_H_
#define EVENTFILEREADER_H_
#include <string>
#include <stdlib.h>
#include <iostream>
class EventFileReader {
public:
EventFileReader(string fileName);
virtual ~EventFileReader();
};
#endif /* EVENTFILEREADER_H_ */
Your compiler is complaining about not being able to find string as a defined type.
You should add its namespace std:
EventFileReader(std::string fileName);
^^^^^
You need to specify namespace, e.g.
std::string
or put the using declaration after includes:
using std::string;
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").
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.