我有一个简单的问题: 这是基类:
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。帮助赞赏
答案 0 :(得分:1)
对于初学者,我强烈建议让自己轻松生活,并将所有实施主体移到单独的文件中(例如,转到CBox.cpp和CCandyBox.cpp)。
这是错误的:
for(unsigned int i = 0; i&lt; strlen(str)+1; i ++) {*(m_Contents + i)= *(str + i); { *(m_Contents + i)= *(str + i); }
这样更好:
for(unsigned int i = 0; i&lt; strlen(str); i ++) m_Contents [i] = str [i]; m_Contents [strlen(str)] ='\ 0';
这更好:
文件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);
...