为另一个类的成员编写一个类的定义(给定代码,不要问我为什么)。无论如何,由于我不熟悉c ++,对象的嵌套以及限定符'::',所以我收到类型'class'重定义错误。
这是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 ++文件。
关于c++ - C++类作为其他类的成员,这是如何定义的?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21423349/