我用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();
};
Run Code Online (Sandbox Code Playgroud)
a.cpp
#include "a.h"
#include "stdafx.h"
class Hex {
public:
Hex(int n, string w) { //some implementation }
//rest class
}
Run Code Online (Sandbox Code Playgroud)
我做错了什么?如果重要的是我正在开发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();
};
Run Code Online (Sandbox Code Playgroud)
.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();
Run Code Online (Sandbox Code Playgroud)
小智 5
在标题中,您将其声明为,Hex();但在 .cpp 中,您将其声明为Hex(int n, string w)
还有为什么不这样定义 Hex::Hex(){//some implementation }
| 归档时间: |
|
| 查看次数: |
1452 次 |
| 最近记录: |