首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++类作为其他类的成员,如何定义它?

C++类作为其他类的成员,如何定义它?
EN

Stack Overflow用户
提问于 2014-01-29 06:06:21
回答 2查看 192关注 0票数 0

为作为另一个类的成员的类编写定义(给定代码,不要问我为什么)。无论如何,我会得到一个类型'class‘重新定义错误,因为我不熟悉c++、对象嵌套和限定符’:‘

这是stack.h

代码语言:javascript
复制
    // 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

代码语言:javascript
复制
    //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

我得到以下代码行的重新定义错误

代码语言:javascript
复制
class stack::UnderFlow
class stack::OverFlow

我肯定这是个简单的解决办法,我只是不练习.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-29 06:09:54

类定义应该出现在头文件中,而不是C++文件中。移除:

代码语言:javascript
复制
class stack::OverFlow
{

来自C++文件。

票数 1
EN

Stack Overflow用户

发布于 2014-01-29 06:26:45

现在你写了

代码语言:javascript
复制
//rest of code above
class stack::OverFlow
{

string message;
OverFlow::OverFlow()
{
}

stack.cpp文件中。在cpp文件中写入类体是不正确的,您应该在头文件中这样做,所以应该删除

代码语言:javascript
复制
class stack::OverFlow
    {

源文件(cpp)。而且这也是在头中完成的(这一项是正确的),所以现在只需要删除提到的部分,并在cpp中添加具有正确名称解析的函数定义,如下所示:

代码语言:javascript
复制
stack::OverFlow::OverFlow()  // constructor
{
}

string stack::UnderFlow::getMessage()  // function definition
{
    return message;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21423349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档