继承问题:在派生类中使用基类的头文件和默认构造函数
我有一个简单的问题: 这是基类:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于初学者,我强烈建议让自己的生活变得轻松,并将所有实现主体移至单独的文件(例如,CBox.cpp 和 CCandyBox.cpp)。
这是错误的:
for(无符号整数 i=0; i< strlen(str)+1; i++)
{ *(m_Contents+i) = *(str +i);
{
*(m_Contents+i) = *(str+i);
for
这样更好:
for(unsigned int i=0; i < strlen(str); i++)
m_Contents[i] = str[i];
m_Contents[strlen(str)] = '\0';
这更好:
文件 CCandyBox.h:
文件 CCandyBox.cpp:
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).
This is wrong:
for(unsigned int i=0; i< strlen(str)+1; i++)
{ *(m_Contents+i) = *(str +i);
{
*(m_Contents+i) = *(str+i);
}
This is better:
for(unsigned int i=0; i < strlen(str); i++)
m_Contents[i] = str[i];
m_Contents[strlen(str)] = '\0';
This is even better:
file CCandyBox.h:
file CCandyBox.cpp: