错误 C2039:“字符串” : 不是'std'的成员,头文件问题

发布于 2024-09-07 11:10:29 字数 1309 浏览 5 评论 0原文

我正在编写的课程遇到问题。我已将类拆分为定义该类的 .h 文件和实现该类的 .cpp 文件。

我在 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;    

};

这是实现文件 FMAT.cpp

#include <iostream>
#include <string>
#include "FMAT.h"

FMAT::FMAT(){

    std::cout << "manually (1) or instruction file (2)\n\n";
    std::cin >> mode;
    if(mode == 2){
        std::cout << "Enter full path name of instruction file\n\n";
        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

}

My无法修复此错误可能是因为我不了解如何将类正确构建到单独的文件中。请随意提供一些有关如何在 C++ 程序中处理多个文件的提示。

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)\n\n";
    std::cin >> mode;
    if(mode == 2){
        std::cout << "Enter full path name of instruction file\n\n";
        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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅笑依然 2024-09-14 11:10:32

注意不要包含

#include <string.h> 

而仅

#include <string>

包含我花了 1 个小时才在我的代码中找到它。

希望这可以帮助

Take care not to include

#include <string.h> 

but only

#include <string>

It took me 1 hour to find this in my code.

Hope this can help

も星光 2024-09-14 11:10:31

您也需要

#include <string>

在头文件中包含此内容。仅靠前向声明还不够。

还要强烈考虑为您的头文件提供头保护,以避免随着项目的增长而出现未来可能出现的问题。所以在顶部做一些类似的事情:

#ifndef THE_FILE_NAME_H
#define THE_FILE_NAME_H

/* header goes in here */

#endif

这将防止头文件被 #included 多次,如果你没有这样的保护,那么你可能会遇到多个声明的问题。

You need to have

#include <string>

in the header file too.The forward declaration on it's own doesn't do enough.

Also strongly consider header guards for your header files to avoid possible future problems as your project grows. So at the top do something like:

#ifndef THE_FILE_NAME_H
#define THE_FILE_NAME_H

/* header goes in here */

#endif

This will prevent the header file from being #included multiple times, if you don't have such a guard then you can have issues with multiple declarations.

℉絮湮 2024-09-14 11:10:31

您的 FMAT.h 需要 std::string 的定义才能完成 FMAT 类的定义。在 FMAT.cpp 中,您已在 #include "FMAT.h" 之前通过 #include 完成了此操作。您还没有在主文件中这样做。

您尝试转发声明 string 在两个层面上都是不正确的。首先,您需要一个完全限定的名称,std::string。其次,这只适用于指针和引用,不适用于声明类型的变量;前向声明没有为编译器提供足够的信息来说明要在您定义的类中嵌入什么内容。

Your FMAT.h requires a definition of std::string in order to complete the definition of class FMAT. In FMAT.cpp, you've done this by #include <string> before #include "FMAT.h". You haven't done that in your main file.

Your attempt to forward declare string was incorrect on two levels. First you need a fully qualified name, std::string. Second this works only for pointers and references, not for variables of the declared type; a forward declaration doesn't give the compiler enough information about what to embed in the class you're defining.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文