当我将 class 移动到头文件时,出现错误 C++
When im moving a class to a header file i get an error C++
这里发生了一些烦人的事情,我希望社区能帮助我 :)。当我在 cpp
文件中有我的 class 时,我的程序工作正常。当我将 class 代码移动到头文件中时,程序会抛出错误。请指导我。谢谢!
.cpp file
#include <iostream>
#include <string>
#include "CSquare.h"
using namespace std;
int main()
{
CSquare alo(1,"name");
}
CSquare.h
#pragma once
class CSquare
{
private:
int squareCode;
string squareName;
public:
CSquare(int, string);
void setCode(int);
void setName(string);
};
CSquare::CSquare(int inputSquareCode, string inputSquareName)
{
setCode(inputSquareCode);
setName(inputSquareName);
}
void CSquare::setCode(int inputSquareCode)
{
squareCode = inputSquareCode;
}
void CSquare::setName(string inputSquareName)
{
squareName = inputSquareName;
}
我也尝试在两个文件中移动 #include string
,但似乎仍然无法解决问题:/
error C3646: 'squareName': unknown override specifier error C4430: missing type specifier - int assumed. Note: C++ does not support default-int error C2061: syntax error: identifier 'string' C2061: syntax
error: identifier 'string' – Mash 16 mins ago
error C2061: syntax error: identifier 'string'
error C2065: 'inputSquareName': undeclared identifier
error C2065: 'string': undeclared identifier error C2146: syntax error: missing ')' before identifier 'inputSquareName'
error C2143: syntax
error: missing ';' before '{'
error C2447: '{': missing function header (old-style formal list?)
error C2661: 'CSquare::CSquare': no overloaded function takes 2 arguments –
CSquare.h
缺少类型 string
的定义。
解决方案:也许您打算使用 std::string
。在这种情况下,您必须在 CSquare.h
中包含 <string>
并使用范围解析运算符来引用 std
命名空间中声明的 string
。有关示例,请参见本段的第一句。
CSquare.h
包含 non-inline 函数的定义。如果 header 包含在多个翻译单元中,那么您违反了 one definition rule.
In the entire program, an object or non-inline function cannot have more than one definition
解决方案:要么在单个源文件中定义函数,要么声明函数 inline
.
这里发生了一些烦人的事情,我希望社区能帮助我 :)。当我在 cpp
文件中有我的 class 时,我的程序工作正常。当我将 class 代码移动到头文件中时,程序会抛出错误。请指导我。谢谢!
.cpp file
#include <iostream>
#include <string>
#include "CSquare.h"
using namespace std;
int main()
{
CSquare alo(1,"name");
}
CSquare.h
#pragma once
class CSquare
{
private:
int squareCode;
string squareName;
public:
CSquare(int, string);
void setCode(int);
void setName(string);
};
CSquare::CSquare(int inputSquareCode, string inputSquareName)
{
setCode(inputSquareCode);
setName(inputSquareName);
}
void CSquare::setCode(int inputSquareCode)
{
squareCode = inputSquareCode;
}
void CSquare::setName(string inputSquareName)
{
squareName = inputSquareName;
}
我也尝试在两个文件中移动 #include string
,但似乎仍然无法解决问题:/
error C3646: 'squareName': unknown override specifier error C4430: missing type specifier - int assumed. Note: C++ does not support default-int error C2061: syntax error: identifier 'string' C2061: syntax
error: identifier 'string' – Mash 16 mins ago
error C2061: syntax error: identifier 'string'
error C2065: 'inputSquareName': undeclared identifier
error C2065: 'string': undeclared identifier error C2146: syntax error: missing ')' before identifier 'inputSquareName'
error C2143: syntax
error: missing ';' before '{'
error C2447: '{': missing function header (old-style formal list?)
error C2661: 'CSquare::CSquare': no overloaded function takes 2 arguments –
CSquare.h
缺少类型 string
的定义。
解决方案:也许您打算使用 std::string
。在这种情况下,您必须在 CSquare.h
中包含 <string>
并使用范围解析运算符来引用 std
命名空间中声明的 string
。有关示例,请参见本段的第一句。
CSquare.h
包含 non-inline 函数的定义。如果 header 包含在多个翻译单元中,那么您违反了 one definition rule.
In the entire program, an object or non-inline function cannot have more than one definition
解决方案:要么在单个源文件中定义函数,要么声明函数 inline
.