我正在努力解决这个任务问题,因为现在3天,老实说,我用完了解决方案。 我创建了类Plant,属性类型的声明,我重载了==& !=运算符,但我经常收到错误消息,无法将参数1从const char [6]转换为Plant? 我不确定我错过了什么?
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class Plant {
private:
char* type[10]; //Declare the type attribute of type Char [10]
Plant(char* type1[10]); //Declare the const type1 of Object Plant
bool operator==(const Plant &b) const
{
return ( (type ==b.type) );
}
bool operator!=(const Plant &b) const
{
return ( (b.type!=type) );
}
//overload the print function
void print()
{ cout << type;
}
};//end class Plant
class Fruit: public Plant {
public:
char* taste [10];
public:
bool operator==(const Fruit& rhs) const
{
//return (taste == rhs.taste && type == rhs.type);
return (taste == rhs.taste);
}
bool operator!=(const Fruit& rhs) const
{ return (taste != rhs.taste );
//return (taste != rhs.taste && type != rhs.type);
}
void print()
{printf("Hi Fruit");
cout << taste;
} // print to know if I reached this part
};//end class Fruit
int main() {
Plant a((char*)"Maple");
a.print();
Plant b("Maple");
if (a == b)
printf("a and b are equal\n");
else
printf("a and b are not equal\n");
if (a != b)
printf("a and b are not equal\n");
else
printf("a and b are equal\n");
Fruit c("Apple","sweet");
c.print();
Fruit d("Apple","sweet");
if (c == d)
printf("c and d are equal\n");
else
printf("c and d are not equal\n");
if (c != d)
printf("c and d are not equal\n");
else
printf("c and d are equal\n");
if (a == c)
printf("a and c are equal\n");
else
printf("a and c are not equal\n");
if (a != c)
printf("a and c are not equal\n");
else
printf("a and c are equal\n");
if (c == a)
std::cout <<"c and a are equal\n"<< std::endl;
else
std::cout <<"c and a are not equal\n"<< std::endl;
if (a != c)
std::cout <<"c and a are not equal\n"<< std::endl;
else
std::cout <<"c and a are equal\n"<< std::endl;
return 0;
}
我是C ++刚开始学习的新手,我已经阅读了以下内容 资源:
任何提示或帮助都将受到高度赞赏..
谢谢!
答案 0 :(得分:1)
这里的错误是由于对C风格字符串的误解造成的,如果您使用std::string
,可能会消失。
例如:
bool operator==(const Plant &b) const
{
return ( (type ==b.type) );
}
不比较两个字符串。它比较了名为type
的指针。您应该使用strcmp
函数。
char* type[10];
这不会创建一个包含9或10个字符的字符串,我认为这是您想要的。它是一个包含10个char
指针的数组。我想你想char type[10]
。这将为一串 9 字符加上空终止符保留空间。如果您尝试存储的内容超过此值,您的程序将显示未定义的行为,甚至可能无法收到错误消息。
(char*)"Maple"
这是一个C风格的演员。不要使用它们。使用static_cast
如果代码没有编译,你可能做错了。永远不要添加演员“只是为了让它编译”。
我强烈建议您使用std::string
,它更像是其他语言的字符串类型,并且更安全。
答案 1 :(得分:1)
我清理了你的代码以使用更多惯用的C ++结构,如std::string
。
#include <iostream>
#include <string>
class Plant {
public:
Plant(std::string name) : type_(name) {}
bool operator==(const Plant &that) const {
return type_ == that.type_;
}
bool operator!=(const Plant &that) const {
return !operator==(that);
}
void print() { std::cout << type_ << std::endl; }
protected:
std::string type_;
};
class Fruit: public Plant {
public:
Fruit(std::string name, std::string taste) : Plant(name), taste_(taste) {}
bool operator==(const Fruit& that) const {
return ((taste_ == that.taste_) && (Plant::operator==(that)));
}
bool operator!=(const Fruit& that) const {
return !operator==(that);
}
void print() { Plant::print(); std::cout << taste_ << std::endl; }
private:
std::string taste_;
};
int main() {
Plant a("Maple");
a.print();
Plant b("Maple");
if (a == b) {
std::cout << "a and b are equal" << std::endl;
} else {
std::cout << "a and b are not equal" << std::endl;
}
Fruit c("Apple","sweet");
c.print();
Fruit d("Apple","sweet");
if (c == d) {
std::cout << "c and d are equal" << std::endl;
} else {
std::cout << "c and d are not equal" << std::endl;
}
if (a == c) {
std::cout << "a and c are equal" << std::endl;
} else {
std::cout << "a and c are not equal" << std::endl;
}
return 0;
}
当我运行它时,我得到了这个输出:
23:24 $ ./a.out
Maple
a and b are equal
Apple
sweet
c and d are equal
a and c are not equal
这是ideone。
答案 2 :(得分:1)
欢迎来到C ++世界。我想你现在已经进入C ++领域,想要摆脱所有那些字符数组和char指针。 std :: string是您可能想要执行的所有基本字符串操作的非常强大的封装。请参阅http://www.cplusplus.com/reference/string/string/以获取std :: string类的完整API定义。基于我对你想要完成的事情的理解,我修改了你的程序以使用std :: string。该程序编译并运行正常。如果您在理解该计划时遇到困难,请告诉我,我可以告诉您。另外,请注意参数化构造函数和初始化列表的使用,您可以在我与您共享的同一网站上找到详细信息。我非常乐意帮助您完成编写C ++程序的任何方面。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class Plant {
private:
std::string type; //Declare the type attribute of type Char [10]
public:
Plant(std::string type1) : type(type1) {}//Declare the const type1 of Object Plant
bool operator==(const Plant &b) const
{
return (type == b.type);
}
bool operator!=(const Plant &b) const
{
return (b.type != type);
}
//overload the print function
void print()
{
cout << type;
}
};//end class Plant
class Fruit: public Plant {
public:
std::string taste;
public:
Fruit(std::string fruit, std::string t) : Plant(fruit), taste(t) {}
bool operator==(const Fruit& rhs) const
{
//return (taste == rhs.taste && type == rhs.type);
return (taste == rhs.taste);
}
bool operator!=(const Fruit& rhs) const
{ return (taste != rhs.taste);
//return (taste != rhs.taste && type != rhs.type);
}
void print()
{printf("Hi Fruit");
cout << taste;
} // print to know if I reached this part
};//end class Fruit
int main() {
Plant a("Maple");
a.print();
Plant b("Maple");
if (a == b)
printf("a and b are equal\n");
else
printf("a and b are not equal\n");
if (a != b)
printf("a and b are not equal\n");
else
printf("a and b are equal\n");
Fruit c("Apple", "sweet");
c.print();
Fruit d("Apple", "sweet");
if (c == d)
printf("c and d are equal\n");
else
printf("c and d are not equal\n");
if (c != d)
printf("c and d are not equal\n");
else
printf("c and d are equal\n");
if (a == c)
printf("a and c are equal\n");
else
printf("a and c are not equal\n");
if (a != c)
printf("a and c are not equal\n");
else
printf("a and c are equal\n");
return 0;
}