+ 1
What is error in this program ???
#include <iostream> #include <string> using namespace std; class car { string name; int speed; public: car(string iname ,int ispeed); { name=iname; speed=ispeed; } void tellme() { cout<<name<<endl<<speed<<endl; } friend void display(car driver); void display(car driver); { cout<<driver.name<<endl; cout<<driver.speed<<endl; } }; int main() { car hello("BMW",250,"red"); display(hello); return (0); }
3 Respuestas
+ 1
thank all of you
0
delete the semicolon in :car(string iname ,int ispeed);
and : void display(car driver);
remove the "red" form car hello("BMW",250,"red");
the constructor cas wants 2 parameters
and try i'm sorry but I don't now c++
0
The main problem is that your class' constructor has 2 parameters and you are calling it with 3 parameters.
Then, you have a semicolon (';') added after your function definitions, which should not be there.
car(string iname ,int ispeed)
{
name = iname;
speed = ispeed;
}
Then, the single purpose of friend functions is for them to be declared outside the class body.
Here is my implementation of your class:
#include <iostream>
#include <string>
using namespace std;
class Car
{
public:
Car(string iname, int ispeed) : name(iname), speed(ispeed)
{
}
void tellme()
{
cout << name << endl << speed << endl;
}
friend void display(Car driver);
private:
string name;
int speed;
};
void display(Car driver)
{
cout << driver.name << endl;
cout << driver.speed << endl;
}
int main()
{
Car hello("BMW", 250);
display(hello);
return (0);
}