I am having problems with a class I am writing. I have split the class into a .h file that defines the class and an .cpp file that implements the class.
I receive this error in Visual Studio 2010 Express:
error C2039: ‘string’ : is not a member of ‘std’
This is the header FMAT.h
class string;
class FMAT {
public:
FMAT();
~FMAT();
int session();
private:
int manualSession();
int autoSession();
int mode;
std::string instructionFile;
};
This is the implementation file FMAT.cpp
#include <iostream>
#include <string>
#include "FMAT.h"
FMAT::FMAT(){
std::cout << "manually (1) or instruction file (2)nn";
std::cin >> mode;
if(mode == 2){
std::cout << "Enter full path name of instruction filenn";
std::cin >> instructionFile;
}
}
int FMAT::session(){
if(mode==1){
manualSession();
}else if(mode == 2){
autoSession();
}
return 1;
}
int FMAT::manualSession(){
//more code
return 0;
}
this is the main file that uses this class
#include "FMAT.h"
int main(void)
{
FMAT fmat; //create instance of FMAT class
fmat.session(); //this will branch to auto session or manual session
}
My inability to fix this error is probably a result of me not understanding how to properly structure a class into separate files. Feel free to provide some tips on how to handle multiple files in a c++ program.
thanks for answer George P,
i install codeblocks 20.03 ,
i select «std:c++17» in language standard ,The error was resolved, but new errors
|
|
У меня проблемы с классом, который я пишу. Я разделил класс на файл .h, который определяет класс, и файл .cpp, который реализует класс.
Я получаю эту ошибку в Visual Studio 2010 Express:
ошибка C2039: ‘строка’: не является членом ‘std’
Это заголовок FMAT.h
class string;
class FMAT {
public:
FMAT();
~FMAT();
int session();
private:
int manualSession();
int autoSession();
int mode;
std::string instructionFile;
};
Это файл реализации FMAT.cpp
#include <iostream>
#include <string>
#include "FMAT.h"
FMAT::FMAT(){
std::cout << "manually (1) or instruction file (2)nn";
std::cin >> mode;
if(mode == 2){
std::cout << "Enter full path name of instruction filenn";
std::cin >> instructionFile;
}
}
int FMAT::session(){
if(mode==1){
manualSession();
}else if(mode == 2){
autoSession();
}
return 1;
}
int FMAT::manualSession(){
//more code
return 0;
}
это основной файл, который использует этот класс
#include "FMAT.h"
int main(void)
{
FMAT fmat; //create instance of FMAT class
fmat.session(); //this will branch to auto session or manual session
}
Моя неспособность исправить эту ошибку, вероятно, является результатом того, что я не понимаю, как правильно структурировать класс в отдельные файлы. Не стесняйтесь дать несколько советов о том, как обрабатывать несколько файлов в программе на C ++.
Я использую VS2008 и Win7 64bit.
#include <iostream>
#include "utils.h"#include <string>
int main()
{
int gm = DETECT;
int gd = DETECT;
initgraph(&gm, &gd, "");
double x = 0;
double y = 0;
double r = 50;
for(int i=0 ; i<=360 ; i++)
{
x = r * cos(DegreeToRad((double)i));
y = r * sin(DegreeToRad((double)i));
PlotLine(0,0,x,y, YELLOW);
std::string str;
str.append(std::to_string(i));
str.append(". ");
str.append(std::to_string(0));
str.append(", ");
str.append(std::to_string(0));
str.append(") to (");
str.append(std::to_string(x));
str.append(", ");
str.append(std::to_string(y));
str.append("). m=");
str.append(std::to_string(Slope(0,0,x,y)));
str.append("n");
if(i%90==0)
{
str.append("ni=");
str.append(std::to_string(i));
str.append("n");
}
WriteToFile("slope.txt", str.c_str());
}
getch();
closegraph();
return 0;
}
Сообщения об ошибках.
1>e:slope.test.cpp(21) : error C2039: 'to_string' : is not a member of 'std'
1>e:slope.test.cpp(21) : error C3861: 'to_string': identifier not found
1>e:slope.test.cpp(23) : error C2039: 'to_string' : is not a member of 'std'
1>e:slope.test.cpp(23) : error C3861: 'to_string': identifier not found
1>e:slope.test.cpp(25) : error C2039: 'to_string' : is not a member of 'std'
1>e:slope.test.cpp(25) : error C3861: 'to_string': identifier not found
1>e:slope.test.cpp(27) : error C2039: 'to_string' : is not a member of 'std'
1>e:slope.test.cpp(27) : error C3861: 'to_string': identifier not found
1>e:slope.test.cpp(29) : error C2039: 'to_string' : is not a member of 'std'
1>e:slope.test.cpp(29) : error C3861: 'to_string': identifier not found
1>e:slope.test.cpp(31) : error C2039: 'to_string' : is not a member of 'std'
1>e:slope.test.cpp(31) : error C3861: 'to_string': identifier not found
1>e:slope.test.cpp(37) : error C2039: 'to_string' : is not a member of 'std'
1>e:slope.test.cpp(37) : error C3861: 'to_string': identifier not found
1>Generating Code...
1>Build log was saved at "file://e:DebugBuildLog.htm"1>RasterizationLineCircleEllipse - 15 error(s), 14 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
-1
Решение
Эти ошибки могут означать:
- std :: to_string требует заголовка, который вы не включили (но не включили)
- Вы не включили C ++ 11 (я не знаю, как это работает для Visual Studio)
- Ваш компилятор не поддерживает C ++ 11.
Из комментариев кажется, что Visual Studio, которую вы используете, не поддерживает C ++ 11, поэтому вы можете использовать старый добрый поток строк и с шаблоном вы можете создать эквивалент std :: to_string:
#include <sstream>
#include <string>
template<class T>
std::string toString(const T &value) {
std::ostringstream os;
os << value;
return os.str();
}
//Usage:
std::string valueStr = toString(10);
valueStr.append(toString(1));
valueStr.append(toString(2.5));
Обратите внимание, что для использования этого типа функции T
должен иметь operator<<
определено, но это не проблема с типами, которые поддерживает std :: to_string.
4
Другие решения
Chempion 0 / 0 / 0 Регистрация: 07.03.2012 Сообщений: 27 |
||||||||
1 |
||||||||
23.03.2012, 09:29. Показов 7337. Ответов 8 Метки нет (Все метки)
Выдает вот такие ошибки
Employee.cpp(20) : error C2039: YearsofService: не является членом «Employee» , никак не пойму что сделать, есть предложения?
Код класса:
__________________
0 |
retmas Жарю без масла 867 / 749 / 225 Регистрация: 13.01.2012 Сообщений: 1,702 |
||||
23.03.2012, 09:46 |
2 |
|||
1 |
Black Fregat Фрилансер 3703 / 2075 / 567 Регистрация: 31.05.2009 Сообщений: 6,683 |
||||
23.03.2012, 10:00 |
3 |
|||
Добавлено через 1 минуту
1 |
0 / 0 / 0 Регистрация: 07.03.2012 Сообщений: 27 |
|
23.03.2012, 10:33 [ТС] |
4 |
Сделал, появилась вот такая ошибка:
0 |
Жарю без масла 867 / 749 / 225 Регистрация: 13.01.2012 Сообщений: 1,702 |
|
23.03.2012, 10:39 |
5 |
ищите в программе гда вы вызываете эти ф-ии без параметра
1 |
0 / 0 / 0 Регистрация: 07.03.2012 Сообщений: 27 |
|
23.03.2012, 11:36 [ТС] |
6 |
И что с ними делать?) если в коде поищешь который выше, буду очень благодарен)
0 |
Фрилансер 3703 / 2075 / 567 Регистрация: 31.05.2009 Сообщений: 6,683 |
|
26.03.2012, 05:29 |
7 |
У Вас ведь на каждый параметр 2 разных функции доступа — для чтения и для записи.
1 |
0 / 0 / 0 Регистрация: 07.03.2012 Сообщений: 27 |
|
27.03.2012, 09:56 [ТС] |
8 |
Спасибо всем, нашел все ошибки и исправил) всё работает)
0 |
0 / 0 / 0 Регистрация: 02.01.2016 Сообщений: 28 |
|
02.01.2016, 12:56 |
9 |
0 |