2015-12-16 51 views
-7
class Fruit{ 

}; 

class Banana: public Fruit 
{ 
    public: 
    bool isRipe(){ 
     if (mColor == ‘y’){ 
      return true; 
     } 
     else{ 
      return false; 
     } 
    } 
}; 

main(){ 
    Banana banana; 
    banana.setColor(‘y’); 
    if(banana.isRipe()){ 
     cout << “banana is ready to eat” << endl; 
    } 
    else{ 
     cout << “banana is not ready to eat” << endl; 
    } 
} 

这是需要编译的代码,但抛出下面的错误: -错误而执行的C++代码

$\fruit.cpp||In member function 'bool Banana::isRipe()':| 
$\fruit.cpp|17|error: 'y' was not declared in this scope| 
$\fruit.cpp||In function 'int main()':| 
$\fruit.cpp|28|error: 'class Banana' has no member named 'setColor'| 
$\fruit.cpp|28|error: 'y' was not declared in this scope| 
$\fruit.cpp|30|error: 'cout' was not declared in this scope| 
$\fruit.cpp|30|error: expected ';' before 'is'| 
$\fruit.cpp|33|error: 'cout' was not declared in this scope| 

$ \ fruit.cpp | 33 |错误:预期 ';'在'is'之前| || ===构建失败:32个错误,0个警告(0分钟,0秒)=== |

我想使用给定派生类的水果的基类,但没有得到它的权利。任何帮助表示赞赏。

+2

请提供实际的代码以及错误。如果我们没有任何一个,我们如何猜测出现了什么问题? – Quentin

+2

_不正确_继续下去并不是很有用。预期产出是多少?目前的产出是多少?你似乎也错过了'setColor'的声明/定义,尽管它大概在'Fruit'类中。 – Tas

+0

为什么你想从一个空白的课程派生。格式化让我想哭:'( –

回答

2

‘y’“banana is ready to eat”看起来可疑:错了引号

他们应该'y'"banana is ready to eat"

编译器要“正常” ""引号来标识字符串和''单个字符

+0

值得一提的是,要调用方法和使用成员变量,首先必须定义方法和成员变量,'mColor'和'setColor'显然不存在。 – user4581301

1

我完全同意fuzzything44Gian Paolo评论。顺便说一下,应该指出的是,当前的ISO C++标准需要main函数的声明有一个类型(例如int main())。

0

你的代码有太多错误,我怀疑你是否已经考虑过它。

$\fruit.cpp||In member function 'bool Banana::isRipe()':|
$\fruit.cpp|17|error: 'y' was not declared in this scope|

@Gian保罗击中头部钉与their answer有关不正确使用引号:

Compiler wants "normal" "" quotation marks to identify strings and '' for single characters.

$\fruit.cpp||In function 'int main()':|
$\fruit.cpp|28|error: 'class Banana' has no member named 'setColor'|

你从字面上没有宣布一个名为setColor功能。也许你的Fruit类是为了声明它(因为所有的水果大概都有一种颜色),否则你的类意味着声明它,无论哪种方式,你需要在使用它之前声明这个函数。在相同的音符,你也没申报mColor

class Fruit 
{ 
public: 
    void setColor(char col) { mColor = col }; 
protected: 
    char mColor; 
}; 

关于这一点,你可能要因为它使超过char更为明智用于此的enumg代表什么颜色,或b

class Fruit 
{ 
public: 
    enum class Color { Yellow, Red, Blue, Green }; 
    void setColor(Color c) { mColor = c; } 
protected: 
    Color mColor; 
}; 

$\fruit.cpp|28|error: 'y' was not declared in this scope|
$\fruit.cpp|30|error: expected ';' before 'is'|

,因为你们没有使用字符周围的正确的行情中,编译器假定它是一个变量。你需要使用单引号:

if (mColor == 'y') 

但是,如果使用enum如上规定,它会流向更顺理成章地进入:

if (mColor == Color::Yellow) 

$\fruit.cpp|30|error: 'cout' was not declared in this scope|
$\fruit.cpp|33|error: 'cout' was not declared in this scope|

你要么不using namespace std或不包括iostream.h,但我打算猜测并说出两者。您需要包括其中std::coutstd::endl声明的文件,并指定他们来自命名空间:

#include <iostream> // cout, endl 
... 
std::cout << "banana is ready to eat" << std::endl; 

还要注意我已经改变了引号双报价为字符串文字。

值得一提的还有两件事:
1.继承一个空的类是没有意义的,就像你在Fruit
2.作为@EuGENE points out in their answer,您需要为所有功能指定返回类型,包括main()! C++,C不同,会以为你的意思是,默认情况下返回int

int main() 

我的建议是采取更多的照顾在未来:慢些,更频繁地编译和读什么编译器告诉你是错误的。