小编Lin*_*uck的帖子

C++ 不能使用另一个文件中的类

我通过编写小程序是 C++。我也是处理多个文件的重点。我一直坚持使用另一个文件中的类。我做了一个简单的测试项目来演示我的问题。我有3个文件。

测试头文件

#ifndef __testheader_H_INCLUDED__   // if Node.h hasn't been included yet...
#define __testheader_H_INCLUDED__   //   #define this so the compiler knows it has been included

#include <string>
#include <iostream>
class testheader { 
    public:
    testheader(std::string name){}
    void write(){}
};
#endif
Run Code Online (Sandbox Code Playgroud)

测试头文件

#include <string>
#include <iostream>

using namespace std;

class testheader {
    public:
    testheader(string name){
        cout << name << endl;
    }
    void write(){
        cout << "stuff" << endl;
    }
};
Run Code Online (Sandbox Code Playgroud)

另一个文件.cpp

#include <iostream>
#include "testheader.h"

using namespace std;

int main () { …
Run Code Online (Sandbox Code Playgroud)

c++ header class file

1
推荐指数
1
解决办法
3660
查看次数

标签 统计

c++ ×1

class ×1

file ×1

header ×1