继承问题:在派生类中使用基类的头文件和默认构造函数

发布于 2024-12-19 05:08:09 字数 2909 浏览 3 评论 0原文

我有一个简单的问题: 这是基类:

class CBox
{
public:

    double m_Length;
    double m_Width;
    double m_Height;
    CBox()
    {

    }
    CBox(double lv=1.0,double wv=1.0,double hv=1.0)
            :m_Length(lv),m_Width(wv),m_Height(hv)
        {
        }
    ~CBox();
protected:
private:
};

这是派生类:

 #ifndef CCANDYBOX_H

    #define CCANDYBOX_H

   #include "CBox.h"

   #include <iostream>

   #include <cstring>


class CCandyBox :CBox
{
    public:
         char* m_Contents;
        CCandyBox(double lv, double mv, double hv, char* str):CBox(lv,mv,hv)
        {
            m_Contents = new char[ strlen(str) +1 ];

            for(unsigned int i=0; i< strlen(str)+1; i++)
            {               *(m_Contents+i) = *(str +i);
            {
                *(m_Contents+i) = *(str+i);
            }

        }
        CCandyBox(char* str = "Candy"):CBox()
        {
              m_Contents = new char[ strlen(str) +1];

              for(unsigned int i=0; i<strlen(str)+1;++i)
              {
                  *(m_Contents +i) = *(str+i);
              }
        }
        virtual ~CCandyBox()
        {
            delete[] m_Contents;
        }
        CCandyBox(const CCandyBox& other);
        CCandyBox& operator=(const CCandyBox& other);
    protected:
    private:
};


#endif // CCANDYBOX_H

Main.cpp 函数仅包含“CCandyBox.h”和一个简单的“helloworld”

并给出以下错误:

        C:\Work\Check\main.cpp|4|error: expected nested-name-specifier before 'namespace'|
C:\Work\Check\main.cpp|4|error: expected unqualified-id before 'namespace'|
C:\Work\Check\main.cpp|4|error: expected ';' before 'namespace'|
C:\Work\Check\main.cpp|4|error: expected unqualified-id before 'namespace'|
C:\Work\Check\main.cpp|11|error: expected '}' at end of input|
C:\Work\Check\CCandyBox.h||In constructor 'CCandyBox::CCandyBox(double, double, double, char*)':|
C:\Work\Check\CCandyBox.h|24|error: expected primary-expression before '(' token|
C:\Work\Check\CCandyBox.h|24|error: expected primary-expression before 'char'|
C:\Work\Check\CCandyBox.h|24|error: expected ';' before ':' token|
C:\Work\Check\CCandyBox.h|41|error: expected '}' at end of input|
C:\Work\Check\main.cpp||In member function 'int CCandyBox::main()':|
C:\Work\Check\main.cpp|9|error: 'cout' was not declared in this scope|
C:\Work\Check\main.cpp|9|note: suggested alternative:|
c:\mingw\bin\..\lib\gcc\mingw32\4.6.1\include\c++\iostream|62|note:   'std::cout'|
C:\Work\Check\main.cpp|9|error: 'endl' was not declared in this scope|
C:\Work\Check\main.cpp|9|note: suggested alternative:|
c:\mingw\bin\..\lib\gcc\mingw32\4.6.1\include\c++\ostream|543|note:   'std::endl'|
C:\Work\Check\main.cpp|11|error: expected unqualified-id at end of input|
||=== Build finished: 16 errors, 0 warnings ===|

我正在将 MinGW 与 CodeBlocks 一起使用。感谢帮助

I have got a simple question:
Here is the base class:

class CBox
{
public:

    double m_Length;
    double m_Width;
    double m_Height;
    CBox()
    {

    }
    CBox(double lv=1.0,double wv=1.0,double hv=1.0)
            :m_Length(lv),m_Width(wv),m_Height(hv)
        {
        }
    ~CBox();
protected:
private:
};

And here is the derived class:

 #ifndef CCANDYBOX_H

    #define CCANDYBOX_H

   #include "CBox.h"

   #include <iostream>

   #include <cstring>


class CCandyBox :CBox
{
    public:
         char* m_Contents;
        CCandyBox(double lv, double mv, double hv, char* str):CBox(lv,mv,hv)
        {
            m_Contents = new char[ strlen(str) +1 ];

            for(unsigned int i=0; i< strlen(str)+1; i++)
            {               *(m_Contents+i) = *(str +i);
            {
                *(m_Contents+i) = *(str+i);
            }

        }
        CCandyBox(char* str = "Candy"):CBox()
        {
              m_Contents = new char[ strlen(str) +1];

              for(unsigned int i=0; i<strlen(str)+1;++i)
              {
                  *(m_Contents +i) = *(str+i);
              }
        }
        virtual ~CCandyBox()
        {
            delete[] m_Contents;
        }
        CCandyBox(const CCandyBox& other);
        CCandyBox& operator=(const CCandyBox& other);
    protected:
    private:
};


#endif // CCANDYBOX_H

Main.cpp function just includes "CCandyBox.h" and a simple "helloworld"

And gives the following errors:

        C:\Work\Check\main.cpp|4|error: expected nested-name-specifier before 'namespace'|
C:\Work\Check\main.cpp|4|error: expected unqualified-id before 'namespace'|
C:\Work\Check\main.cpp|4|error: expected ';' before 'namespace'|
C:\Work\Check\main.cpp|4|error: expected unqualified-id before 'namespace'|
C:\Work\Check\main.cpp|11|error: expected '}' at end of input|
C:\Work\Check\CCandyBox.h||In constructor 'CCandyBox::CCandyBox(double, double, double, char*)':|
C:\Work\Check\CCandyBox.h|24|error: expected primary-expression before '(' token|
C:\Work\Check\CCandyBox.h|24|error: expected primary-expression before 'char'|
C:\Work\Check\CCandyBox.h|24|error: expected ';' before ':' token|
C:\Work\Check\CCandyBox.h|41|error: expected '}' at end of input|
C:\Work\Check\main.cpp||In member function 'int CCandyBox::main()':|
C:\Work\Check\main.cpp|9|error: 'cout' was not declared in this scope|
C:\Work\Check\main.cpp|9|note: suggested alternative:|
c:\mingw\bin\..\lib\gcc\mingw32\4.6.1\include\c++\iostream|62|note:   'std::cout'|
C:\Work\Check\main.cpp|9|error: 'endl' was not declared in this scope|
C:\Work\Check\main.cpp|9|note: suggested alternative:|
c:\mingw\bin\..\lib\gcc\mingw32\4.6.1\include\c++\ostream|543|note:   'std::endl'|
C:\Work\Check\main.cpp|11|error: expected unqualified-id at end of input|
||=== Build finished: 16 errors, 0 warnings ===|

I am using MinGW with CodeBlocks. Help appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁对谁错谁最难过 2024-12-26 05:08:09
  1. 对于初学者,我强烈建议让自己的生活变得轻松,并将所有实现主体移至单独的文件(例如,CBox.cpp 和 CCandyBox.cpp)。

  2. 这是错误的:

    for(无符号整数 i=0; i< strlen(str)+1; i++)
    { *(m_Contents+i) = *(str +i);
    {
    *(m_Contents+i) = *(str+i);
    for

  3. 这样更好:

    for(unsigned int i=0; i < strlen(str); i++)
    m_Contents[i] = str[i];
    m_Contents[strlen(str)] = '\0';

  4. 这更好:

文件 CCandyBox.h:

#ifndef CCANDYBOX_H
#define CCANDYBOX_H

#include <string>
#include "CBox.h"

class CCandyBox : public CBox
{
    public:
    std::string m_Contents;
    ...

文件 CCandyBox.cpp:

#include <string.h>
#include "CCandyBox.h"
...
CCandyBox::CCandyBox(double lv, double mv, double hv, char* str)
  : CBox(lv,mv,hv)
{
  m_Contents = std::string(str);
  ...
  1. For starters, I'd strongly suggest making life easy on yourself and move all your implementation bodies to separate files (for example, to CBox.cpp and CCandyBox.cpp).

  2. This is wrong:

    for(unsigned int i=0; i< strlen(str)+1; i++)
    { *(m_Contents+i) = *(str +i);
    {
    *(m_Contents+i) = *(str+i);
    }

  3. This is better:

    for(unsigned int i=0; i < strlen(str); i++)
    m_Contents[i] = str[i];
    m_Contents[strlen(str)] = '\0';

  4. This is even better:

file CCandyBox.h:

#ifndef CCANDYBOX_H
#define CCANDYBOX_H

#include <string>
#include "CBox.h"

class CCandyBox : public CBox
{
    public:
    std::string m_Contents;
    ...

file CCandyBox.cpp:

#include <string.h>
#include "CCandyBox.h"
...
CCandyBox::CCandyBox(double lv, double mv, double hv, char* str)
  : CBox(lv,mv,hv)
{
  m_Contents = std::string(str);
  ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文