C 类是其他类的成员,一个人如何定义这个
C++ class as member of other class, How does one define this?
编写是另一个类的成员(给定代码,不要问我为什么)的类的定义。无论如何,我会得到类型的"类"重新定义错误,因为我不熟悉C ,对象的嵌套和预选赛'::'
这是stack.h
// rest of code above
public: // prototypes to be used by the client
/** UnderFlow Class
* thrown if a pop or top operation would cause the stack to underflow
*/
class OverFlow
{
public:
/*! @function OverFlow Constructor
* @abstract constructs the overflow object
* @param string the error message of the overflow exception
*/
OverFlow(std::string);
/*! @function getMessage
* @abstract returns the message containing this exception's error text
* @result string the error message of the overflow exception
*/
std::string getMessage();
private:
std::string message;//error text
};
/** UnderFlow Class
* thrown if a pop or top operation would cause the stack to underflow
*/
class UnderFlow
{
public:
/*! @function UnderFlow Constructor
* @abstract constructs the underflow object
* @param string the error message of the underflow exception
*/
UnderFlow(std::string);
/*! @function getMessage
* @abstract returns the message containing this exception's error text
* @result string the error message of the underflow exception
*/
std::string getMessage();
//rest of code below
这是stack.cpp
//rest of code above
class stack::OverFlow
{
string message;
OverFlow::OverFlow()
{
}
OverFlow(string errormessage)
{
OverFlow::message = errormessage;
}
string OverFlow::getMessage()
{
return message;
}
};
class stack::UnderFlow
{
string message;
UnderFlow::UnderFlow()
{
}
UnderFlow::UnderFlow(string errormessage)
{
message = errormessage;
}
string UnderFlow::getMessage()
{
return message;
}
};//rest of code below
我在代码的以下行中获得重新定义错误
class stack::UnderFlow
class stack::OverFlow
我敢肯定这是一个简单的修复,我只是无法实践...
类定义应显示在标题文件中,而不是C 文件中。删除:
class stack::OverFlow
{
来自C 文件。
当前您写了
//rest of code above
class stack::OverFlow
{
string message;
OverFlow::OverFlow()
{
}
在stack.cpp
文件中。这是在CPP文件中写入类主体的正确性,您应该在标题中进行操作,因此您应该删除
class stack::OverFlow
{
来自源文件(cpp
)。而且您也可以在标题中完成此操作(这是正确的),因此现在您只需要删除上述零件并在cpp
中使用正确的名称分辨率添加函数的定义,例如:
stack::OverFlow::OverFlow() // constructor
{
}
string stack::UnderFlow::getMessage() // function definition
{
return message;
}
相关文章:
- 在类定义之后定义一个私有方法
- Android Studio:如何在build.gradle中定义自定义宏(针对不同的构建变体),并让原生C / C++
- 如何定义自定义生成配置类型,其中通常是.exe的目标改为 DLL
- 嵌套定义与定义的参数 C++
- C++定义自定义unordered_set的不同方法
- C++ - 重新定义/以前定义的错误
- 是否有解决方法可以在 c++ 中为 short 定义用户定义的文字?
- 如何为 std::unordered_set 定义自定义键等效谓词?
- 当类和函数具有单独的模板参数时,在类定义之外定义友元函数
- 如何使用 QListIterator 的声明自定义类型定义
- 类型定义重定义错误
- 如何从设备代理定义自定义片段
- 如何定义自定义跨平台size_t类型
- C++最接近追溯定义已定义类的超类是什么?
- boost :: fibonacci_heap:带有比较器重新定义圆形定义错误的嵌套定义
- C++ - 定义自定义新建和删除运算符时make_shared
- 用于定义类型定义的可变参数模板(使用 C++11)
- 我如何使用explicit模板实例化用于类定义中定义的模板成员函数
- 定义未定义的行为
- 定义具有定义(x)的宏