Я пытаюсь решить этот вопрос назначения с 3 дней и, честно говоря, у меня закончились решения. Я создал объявление класса Plant, тип атрибута, и я перегрузил операторы == &! =, Но я постоянно получаю сообщение об ошибке, не могу преобразовать аргумент 1 из const char [6] в Plant? Я не уверен, что мне не хватает?Невозможно преобразовать аргумент 1 из 'const char [6]' в Object
#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++ только начал изучать, и я прочитал следующие ресурсы:
- http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/relational.html
- http://www.cplusplus.com/doc/tutorial/basic_io/ и многие другие сайты, но я не разрешается размещать более 2.
Любой совет или помощь будут высоко оценены ..
Спасибо!
Рекомендации: не используйте 'char *', не используйте 'printf'. – erip
@erip спасибо за прохождение, я попытаюсь отредактировать свой код и, надеюсь, исправить это, но только примечание стороны, мы должны добавить код к классу Plant and Class Fruit. Основной класс не следует трогать его. – Lucyia
Все функции-члены 'Plant' являются частными, включая конструктор. Таким образом, класс 'Plant' не может быть создан, кроме как с помощью функции-члена. Основная функция - не такой член. –