Qt Application will not compile - c++

This has been stumping me for a while. I am trying to create a function that takes a hash table, and returns said hash table. However I am getting this error in the header file,
error: ‘string’ was not declared in this scope.
error: template argument 1 is invalid
Here is the header file itself:
#ifndef NAME_SPAWN_H
#define NAME_SPAWN_H
#include <QString>
#include <QHash>
#include <string>
class Name_Spawn
{
public:
Name_Spawn();
void initalize();
private:
QString int_2_str(int);
void seed();
QHash<string,QString> setTable(QHash<string,QString>);
};
#endif // NAME_SPAWN_H
As you can see, string has been declared. Any ideas? I am at my wits end.

The real name of string is std::string. Try using that instead.
(You can leave off the std:: qualifier only if there's a using namespace std; directive in scope. But it's a good habit not to put using namespace std; in header files.)

Related

I do not understand the error I am getting when trying to use a string as a function parameter

#ifndef MENU_H
#define MENU_H
#include <cstring>
#include <string>
#include <cctype>
class Menu{
public:
Menu();
void run();
int selectOptions();
void rotate90();
void rotate180();
void rotate270();
void flipVert();
void flipHoriz();
void convertHigh(int threshold);
void saveImage(string saveName);
void loadImage(string loadName);
void displayMenu();
private:
}
#endif // MENU_H
C:\Users\Wattenphul\Documents\alg-prog design\project04>g++ -std=c++11 -o main.exe main.cpp menu.cpp
In file included from menu.cpp:6:
menu.h:22:20: error: 'string' has not been declared
void saveImage(string saveName);
^~~~~~
menu.h:22:35: error: expected ',' or '...' before 'saveName'
void saveImage(string saveName);
^~~~~~~~
menu.h:23:20: error: 'string' has not been declared
void loadImage(string loadName);
^~~~~~
menu.h:23:35: error: expected ',' or '...' before 'loadName'
void loadImage(string loadName);
^~~~~~~~
menu.h:28:2: error: expected ';' after class definition
}
this is the code followed by part of the compilation. It is just the header file the rest of the code hasn't been implemented yet. I do not understand why the string is not working. i tried using the resolution operator as well, but that didn't change the result.
It is not called string. It is called std::string.
You may have seen some examples where it was called string. That's because the author took a shortcut and wrong using namespace std somewhere, though we don't necessarily recommend doing that.
string is defined inside namespace std, in order to use you need to prefix it with std, so use std::string instead of just string. Alternatively you can avoid prefixing it using using-directive or using-declaration
Either use std::string instead of string or outside class open namespace "using namespace std;"
But as good coding practice, it is not preferred to open namespace at least in .h file.

C++ error code C2065: '<class name>' undeclared identifier, even though it should be declared in another .h-file

I have a multifile program, and I can't figure out why my program says that "Customers" (in the registerNewUser() function) is an undeclared identifier.
proc.h
#ifndef PROC_H
#define PROC_H
#include <iostream>
#include "const.h"
#include "customers.h"
#include <fstream>
using namespace std;
void registerNewUser(Customers cBase); // Add new user.
#endif // !PROC_H
I have included the header file (customers.h) with the Customers class also.
customers.h
#ifndef CUSTOMERS_H
#define CUSTOMERS_H
#include <iostream>
#include "const.h"
#include "proc.h"
#include "customer.h"
using namespace std;
class Customers {
private:
char* current;
List* customerList; // List for customers.
public:
Customers(); // Constructor.
~Customers(); // Destructor.
void handler(); // Customers handler/menu.
void addNew(char username[]);
};
#endif // !CUSTOMERS_H
Can anyone see what's wrong?
You have a circular include. customers.h includes proc.h so basiacally
void registerNewUser(Customers cBase);
Will get added to customers.h before the compiler has seen what a Customer is. It looks like you should just be able to remove the #include "proc.h" in customers.h and it should compile.
As stated in the comments above you should never use using namespace std; in a header file as anything that includes it now has the entire std namespace exposed. You should also get in the habit of only using it in the most narrow scope you can or drop it completely. For further reading on the use of using namespace std; see Why is “using namespace std” in C++ considered bad practice?
Basically including "customers.h" in "customers.h" wouldn't be a problem here, since you have a guard (plus point for that). Nevertheless it is not very nice.
As NathanOliver said it COULD be a problem with the order of the includes but it doesn't have to. If you include proc.h first everything is fine. If you include customers first, the compiler includes proc.h before he sees the customer class. proc then wont include customers.h (since its guard prevents it). Then he will find your function not knowing what "Customer" means. So depending on the include order of your header files it will or will not work.
If you want a hint: I normally first only include the necessary files for a forward declaration, then do a forward declaration. Then I include the files necessary files for the definition of the class (These will already know that the class exists). The complete class declaration (with member function declaration) follows. If you do it like this you can avoid many mistakes. In your case:
#ifndef CUSTOMERS_H
#define CUSTOMERS_H
class Customers;
#include "proc.h"
#include "ListTool2B.H"
using namespace std;
class Customers
{
private:
char* current;
List* customerList; // List for customers.
public:
Customers(); // Constructor.
~Customers(); // Destructor.
void handler(); // Customers handler/menu.
void addNew(char username[]);
};
#endif
This is probably a duplicate: you have proc.h including customers.h and customers.h including proc.h this will cause a circular reference, and looks like proc.h included in customers is not necessary, so you could try simply to delete this line:
#include "proc.h"

Expected class-name before ‘{’ token

I'm receiving the error:
In file included from proprietario.h:5,
from veiculo.h:4:
motocicleta.h:8: error: expected class-name before ‘{’ token
Motocicleta.h:
#ifndef __MOTOCICLETA__
#define __MOTOCICLETA__
#include <iostream>
#include "veiculo.h"
#include "proprietario.h"
using namespace std;
class Proprietario;
class Motocicleta:public Veiculo{
public:
Motocicleta(int nPassageiros, string modelo, string placa, int aFabricacao, Proprietario* pai, int nRodas, int aro);
~Motocicleta();
Motocicleta (const Motocicleta& source);
Motocicleta& operator= (const Motocicleta& source);
string toString();
};
#endif
Proprietario.h
#ifndef __PROPRIETARIO__
#define __PROPRIETARIO__
#include <iostream>
#include "motocicleta.h"
#include "caminhao.h"
#include "carreta.h"
#include "carro.h"
using namespace std;
class Carro;
class Carreta;
class Caminhao;
class Motocicleta;
class Proprietario{
protected:
string nome;
string cpf;
Motocicleta* mMoto;
Caminhao* mCaminhao;
Carreta* mCarreta;
Carro* mCarro;
};
Veiculo.h:
#ifndef __VEICULO__
#define __VEICULO__
#include <iostream>
#include "proprietario.h"
#include "roda.h"
#include "motor.h"
using namespace std;
class Motor;
class Proprietario;
class Veiculo{
protected:
int nPassageiros;
string modelo;
string placa;
int aFabricacao;
Proprietario* pai;
Roda* rodas;
Motor* mMotor;
int nRodas;
};
I removed the methods, because if i added those the question will be to long, sorry, the code is in PT-BR.
I saw that the problem is usually is forward declaration.
But i cannot find out the problem, i looked in so many forums but i cannot find out the problem..
Someone can help me?
Need any other part of the code?
The real problem here is the liberal use of #include preprocessor directives. In general, you should only include a header file at the lowest scope at which it is needed and forward-declare everything you can. You simply don't (shouldn't) generally need full class declarations for header files. Header files do not generally need to know about implementation details. You definitely shouldn't been forward-declaring and including the header.
As the code stands in the question at the time of writing this answer, you have a circular dependency on veiculo.h (and also on proprietario.h). As veiculo.h really is needed for the header of its subclass Motocicleta, you should remove the #include directives for the classes you have already forward-declared in each of the headers. You can then include the headers in the source files as needed.

Why am I getting string does not name a type Error?

game.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "game.h"
#include "board.h"
#include "piece.h"
using namespace std;
game.h
#ifndef GAME_H
#define GAME_H
#include <string>
class Game
{
private:
string white;
string black;
string title;
public:
Game(istream&, ostream&);
void display(colour, short);
};
#endif
The error is:
game.h:8 error: 'string' does not name a type
game.h:9 error: 'string' does not name a type
Your using declaration is in game.cpp, not game.h where you actually declare string variables. You intended to put using namespace std; into the header, above the lines that use string, which would let those lines find the string type defined in the std namespace.
As others have pointed out, this is not good practice in headers -- everyone who includes that header will also involuntarily hit the using line and import std into their namespace; the right solution is to change those lines to use std::string instead
string does not name a type. The class in the string header is called std::string.
Please do not put using namespace std in a header file, it pollutes the global namespace for all users of that header. See also "Why is 'using namespace std;' considered a bad practice in C++?"
Your class should look like this:
#include <string>
class Game
{
private:
std::string white;
std::string black;
std::string title;
public:
Game(std::istream&, std::ostream&);
void display(colour, short);
};
Just use the std:: qualifier in front of string in your header files.
In fact, you should use it for istream and ostream also - and then you will need #include <iostream> at the top of your header file to make it more self contained.
Try a using namespace std; at the top of game.h or use the fully-qualified std::string instead of string.
The namespace in game.cpp is after the header is included.
You can overcome this error in two simple ways
First way
using namespace std;
include <string>
// then you can use string class the normal way
Second way
// after including the class string in your cpp file as follows
include <string>
/*Now when you are using a string class you have to put **std::** before you write
string as follows*/
std::string name; // a string declaration

C++ string in classes

I know this is quite a ridiculous question but this is quite confusing and irritating, as something that should work simply is not. I'm using Code Blocks with the GCC compiler and I am trying to simply create a string variable in my class
#ifndef ALIEN_LANGUAGE
#define ALIEN_LANGUAGE
#include <string>
class Language
{
public:
private:
string str;
};
#endif
Strange enough, my compiler halts me with an error saying this:
C:\Documents and Settings\...|11|error: `string' does not name a type|
||=== Build finished: 1 errors, 0 warnings ===|
For some reason, it is unable to find the class "string" which for some reason, my main.cpp is able to detect "#include " while my language class is not able for some reason.
This is the main I wrote quickly just to see it main itself is able to see the string file:
//main.cpp
#include <iostream>
#include <string>
#include "alien_language.h"
using namespace std;
int main()
{
string str;
return 0;
}
Does anyone know what's going on?
using namespace std;
That's what's going on.
You don't have std:: prefixing the string in your class. Everything in the standard library is in the namespace std.
It is generally regarded as bad practice to use using namespace std;, by the way. For more information on why and what to do instead, check out this question: Using std Namespace.
The string class is defined in the std namespace. You should chenge the class to this:
class Language
{
public:
private:
std::string str;
};
It is also possible, but not recommended to add this to the top of the header file:
using namespace std;
string is in namespace std, and you need to qualify it fully inside your header file:
#include <string>
class Language
{
public:
private:
std::string str;
};
Do not use using namespace std; or similar in header files.
You should refer to it as std::string;
It looks to me like you're missing the all-important (with a hint of sarcasm) using namespace std; line. Either add that in before your class, or explicitely use std::string str. I'd recommend against adding the using namespace std; line in a header file, as it would pollute the mainspace for any file that includes it.
The string class in standard C++ is in std namespace. Write something like
using std::string; in your header or fully qualify it as std::string in your header.
Beware that using namespace std; in header is a bad practice (read here).