- 错误:
error: 'c' was not declared in this scope
- 原因:在调用getcolor方法时,使用了未定义的c变量
- 解决:直接去掉c对象,直接调用color
- 源代码
#include <iostream>
#include <string>
using namespace std;
class Car{
string color;
friend void changeCar(Car &c);
public:
string getColor(){
return c.color;
}
};
void changeCar(Car& c){
c.color = "红色";
}
int main() {
Car c;
changeCar(c);
cout << c.getColor() << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Car{
string color;
friend void changeCar(Car &c);
public:
string getColor(){
return color;
}
};
void changeCar(Car& c){
c.color = "红色";
}
int main() {
Car c;
changeCar(c);
cout << c.getColor() << endl;
return 0;
}