为作为另一个类的成员的类编写定义(给定代码,不要问我为什么)。无论如何,我会得到一个类型'class‘重新定义错误,因为我不熟悉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我肯定这是个简单的解决办法,我只是不练习.
发布于 2014-01-29 06:09:54
类定义应该出现在头文件中,而不是C++文件中。移除:
class stack::OverFlow
{来自C++文件。
发布于 2014-01-29 06:26:45
现在你写了
//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;
}https://stackoverflow.com/questions/21423349
复制相似问题