C ++ .cpp文件看不到.h中的变量

命名

我用c ++编写程序。首先,我已经正常地编写了它(通常我不是用C ++编写的),我想将变量放在标头中,并将代码放在.cpp文件中。问题是.cpp中的类看不到变量-“标识符未定义”。

#include <iostream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;

class Hex {

private:
    int n;
    string value;
    bool negative = false;

public:
     Hex();
     bool isCorrect();
     string getValue();
     void setValue();
};

cpp文件

#include "a.h"
#include "stdafx.h"     

class Hex {

    public:
      Hex(int n, string w) { //some implementation }

//rest class
}

我做错了什么?如果重要的话,我正在研究VS 2013。

戴着帽子的公鸡

您要定义两次类,一次在头文件中,一次在.cpp文件中。假设您只想在头文件中声明函数并在.cpp文件中定义它们,这就是方法:header:

#include <iostream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;

class Hex {

private:
    int n;
    string value;
    bool negative;

public:
     Hex(int n, string w);
     bool isCorrect();
     string getValue();
     void setValue();
};

.cpp文件:

#include "a.h"
#include "stdafx.h"     
Hex::Hex(int n, string w) : negative(false) { /*some implementation*/ }
//rest class and definitions of bool isCorrect(); string getValue(); void setValue();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章