Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am new in C++. When I compile this code compiler reports error-
main.cpp-
#include <iostream>
#include <string>
#include "main.h"
using namespace std;
string strings::getstr(string str)
{
return str;
}
int main()
{
strings strstr;
string constr;
string msg;
msg = "Hello World!";
constr = strstr.getstr(msg);
cout << constr;
return 0;
}
main.h-
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
#include <string>
class strings
{
public:
string getstr (string str);
};
#endif // MAIN_H_INCLUDED
error-
error: 'string' does not name a type
error: no 'std::string strings::getstr(std::string)' member function declared in class 'strings'
error: In function 'int main()':
error: 'class strings' has no member named 'getstr'
I am using Code::Blocks and gcc
I have written this simple code because I am working on a project and when I want to compile I always getting
'string' does not name a type
sorry for bad english...
The correct name for the string class is 'std::string' because it is declared within the 'std' namespace (the same goes for 'cout'). After you change 'string' into 'std::string' and use 'std::cout' instead of 'cout' your program will compile correctly.
Another way to do it is to place 'std' the as the first namespace:
#include <string>
using namespace std;
Personally, I don't like to use 'using namespace ...' (It's difficult for me to keep track of different namespaces ).
This could fix the problem
#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED
#include <string>
class strings
{
public:
std::string getstr(std::string str);
};
#endif // MAIN_H_INCLUDED
use using namespace std; after the header in main.h
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I will write the code of the header file and two .cpp files I created under the same folder below. And I tried this under Visual Studio, gcc-gnu, and dev c++. I am getting the following error in the file employe.cpp:
expected initializer before '.' token
employee.h:
#ifndef employee
#define employee
#include <iostream>
using namespace std;
class employe{
public:
string name;
int id;
int salary;
void showInfos();
};
#endif
employe.cpp:
#include "employee.h"
#include <iostream>
using namespace std;
void employe.showInfos(){
cout<<"Ad:"<<employe.name<<endl<<"Id:"<<employee.id<<endl<<"Salary:"<<employee.salary;
}
main.cpp:
#include <iostream>
#include "employee.h"
#include <iostream>
int main(){
employe.id=21;
cout<<employe.id;
return 0;
}
main.cpp errors:
expected unqualified-id before '.' token
expected primary-expression before '.' token
I meant to create my own header file and use it. But this happened.
employe.cpp fails because you are qualifying everything incorrectly. It needs to look more like this:
#include "employee.h"
#include <iostream>
using namespace std;
void employe::showInfos(){
cout << "Ad:" << name << endl << "Id:" << id << endl << "Salary:" << salary;
}
main.cpp fails because none of your struct members are static so you can't access any of them on the struct type itself, like you are trying to do. You need to declare an object instance of employe, eg:
#include "employee.h"
int main(){
employe emp;
emp.name = "Joe";
emp.id = 21;
emp.salary = 12345;
emp.showInfos();
return 0;
}
Online Demo
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
In C++ the following header file is legal
#ifndef SAMPLE_H_
#define SAMPLE_H_
class Sample {
private:
int number;
};
#endif
But the following header file is illegal
#ifndef
#define
class Sample {
private:
string name;
};
#endif
Why is it like that?
In my case I have the following header file:
Alphabet.h
#include <string>
#ifndef ALPHABET_H_
#define ALPHABET_H_
class Rhyme {
private:
string a;
public:
Rhyme ();
};
#endif
Alphabet.cpp
#include <iostream>
#include "Alphabet.h"
using namespace std;
Rhyme::Rhyme () {
a = "A for Apple";
}
Main.cpp
#include <iostream>
#include "Alphabet.h"
using namespace std;
int main () {
Rhyme rhyme;
return 0;
}
Linux terminal command:
g++ *.cpp
./a.out
After this I am getting the following error:
Error:
In file included from Alphabets.cpp:2:0:
Alphabet.h:10:2: error: ‘string’ does not name a type
string a;
^
Alphabets.cpp: In constructor ‘Rhyme::Rhyme()’:
Alphabets.cpp:8:2: error: ‘a’ was not declared in this scope
a = "A for Apple";
^
In file included from Main.cpp:2:0:
Alphabet.h:10:2: error: ‘string’ does not name a type
string a;
I am trying to declare a string member variable in header file as private, and then initialize it from another file using constructor
In C++, int is a builtin keyword and is a valid type anywhere in the code. string is a class in the std namespace defined in the <string> header and may only be used if you include the header first.
You should not use the using namespace directive in header files (namespace pollution), so you need to write std::string.
Also, use the file name of your header (e.g. SAMPLE_H) for include guard:
#ifndef SAMPLE_H
#define SAMPLE_H
#include <string>
class Sample {
private:
std::string name;
};
#endif
std::string is a standard user-defined class that is declared in the header <string>. So you need to include the header
#include <string>
and is placed in the standard name space std.
So you need at least write
class Sample {
private:
std::string name;
};
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I keep getting the following errors on every line containing list<string> below:
ISO C++ forbids declaration of 'list' with no type
expected ';' before '<' token
#ifndef __REGNAMEGENERATOR_H
#define __REGNAMEGENERATOR_H
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <sstream>
#define Max_reg_Num 1000
using namespace std;
class RegNameGenerator{
private:
int intRegNumber;
int realRegNumber;
list<string> UsedIntReg; // error
list<string> UsedRealReg; // error
public:
RegNameGenerator();
~RegNameGenerator();
string generateIntReg();
string generateRealReg();
list <string> getUsedIntReg(); // error
list <string> getUsedRealReg(); // error
int getIntRegNum();
int getRealRegNum();
};
#endif
You have to include header <list>:
#include <list>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Question: I have delcared a class, then used in my main function, it throws undefined error. Then I added .h file, and it throws another error. My question is about to write .h in accordance with the .cpp class file. Here are details:
I have this class declated in ElevatorButton.cpp:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class ElevatorButton
{
public:
int pressed = 0; // boolean
void myFunc(int param1)
{
cout << param1 << endl;
}
};
And then I have this file in ElevatorSimulation.cpp:
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Header.h"
using namespace std;
int main()
{
ElevatorButton ElvBtn;
cout << ElvBtn.myFunc(1) << endl;
}
Compiling the code regularly throws error. I should declare header file as it seems. But in my header file, how should I manage the definition? I have read about .h file but still am confused. Here is my header.cpp:
#ifndef ElevatorButton_H
#define ElevatorButton_H
class ElevatorButton
{
public:
int pressed = 0; // boolean
public:
void myFunc(int param1){};
};
#endif
But it throws the following error with the Header.h:
1>ElevatorSimulation.cpp(14): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
You're getting that error because myFunc returns void, so
cout << ElvBtn.myFunc(1)
doesn't have a definition when the right hand side evaluates to void. If you want to print a value with cout, myFunc will have to return a type that can be passed into a stream.
The issue isn't with your class definition at all, it's in your main function.
Is it header.cpp? Because it should be header.h
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I tried testing MinGW under Windows 7 with a simple Hello World program and got the following errors:
C:\code\helloworld.cpp:2:2: error: invalid preprocessing directive #INCLUDE
C:\code\helloworld.cpp:3:7: error: expected neested-name-specifier before 'namespace'
C:\code\helloworld.cpp:3:17: error: expected ';' before 'std'
C:\code\helloworld.cpp:3:17: error: 'std' does not name a type
C:\code\helloworld.cpp: In function 'int main()':
C:\code\helloworld.cpp:7:2: error: 'cout' was not declared in this scope
My original code was as follows:
//Hello, World
#INCLUDE <iostream>
using namesapce std;
int main()
{
cout << "Hello, world!";
return 0;
}
#include should be lower case. C++ is case sensitive.
It should be lowercase. Use #include.
Also, it's using namespace std; (typo in namespace).
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!";
return 0;
}