gpt4 book ai didi

c++ - 继承问题 : Using header files and default constructor of the base class in the derived class

转载 作者:行者123 更新时间:2023-11-28 08:13:34 25 4
gpt4 key购买 nike

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

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。帮助表示赞赏

最佳答案

  1. 对于初学者,我强烈建议让自己的生活变得轻松,并将所有实现主体移动到单独的文件(例如,移动到 CBox.cpp 和 CCandyBox.cpp)。

  2. 这是错误的:

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

  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);
...

关于c++ - 继承问题 : Using header files and default constructor of the base class in the derived class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8373610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com