String could not be resolved c++ - c++

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;

Related

C++ header file with stack variable

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

Parse issue expected ')' C++ compile error

I don't see what's wrong with the following code. WordStore.cxx is defined similarly to have an empty function body. The compiler is complaining that "expected ')'" in the semstore.h function definition. I'm using XCode.
Incidentally, I'm upgrading some ancient (10+ year-old) code to compile on a modern C++ compiler.
/* WordStore.h */
#ifndef WORD_STORE_H
#define WORD_STORE_H
class WordStore
{
public:
WordStore();
};
#endif
// semclass.h
#ifndef SEMCLASS_H
#define SEMCLASS_H
#include <iostream>
using namespace std;
void ReadSemRules(std::istream& stream, WordStore& ws);
#endif
// semclass.cxx
#include <iostream>
#include <string.h>
#include "WordStore.h"
#include "SemClass.h"
using namespace std;
void ReadSemRules(istream& stream, WordStore& ws)
{
}
You have stray unprintable character in your program between the m and the &:
https://godbolt.org/g/gAAoGn
void ReadSemRules(std::istream& stream, WordStore& ws);
^^

error: invalid use of incomplete type ‘struct Item’

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;

Compiling Helper file with functions

I'm at a loss - i'm just getting into C++ and for some reason this is not working out for me. So i'm using Netbeans, and i've got the following main file:
#include <cstdlib>
#include "functions.h"
using namespace std;
int main(int argc, char** argv) {
f("help");
return 0;
}
Functions.h file:
#include <string>
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
void f( string a );
#endif
and Functions.cpp file:
#include "functions.h"
void f( string a ) {
return;
}
So, long story short, it doesn't compile. It says it can't understand the string variable? I don't get it, i tried moving the include for string all over the place but nowhere seems to help. What do i do?
If you are attempting to use std::string, you have to #include <string> in your functions header, and call it std::string,since it is in the std namespace.
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <string>
void f( std::string a );
#endif
See this related post and also why is 'using namespace std' considered bad practice in C++?
You need to include string header file in Functions.h, also tell compiler that string is from std namespace.
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <string>
void f( std::string a );
#endif
Functions.cpp file:
#include "functions.h"
void f( std::string a ) {
return;
}
Better practice is to pass string by const reference
void f(const std::string& a ) {
return;
}
See Why is 'using namespace std;' considered a bad practice in C++?
Include the standard header: <string>
#include <string>

Class definition and utilization errors

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").