所以我搜索了高低,也许我不知道我想问什么问题,如果是这样,请帮我弄清楚可能是什么。
我正在尝试使用man类中的myMan对象中的函数从car类中打印出myCar对象的颜色。我(想)我把男人班作为汽车班的朋友所以我不知道为什么我不能访问汽车私人颜色变量。有人能告诉我为什么我无法访问汽车私人颜色变量?我必须使用朋友。
我一直试图这样做超过20个小时,而且我的智慧结束了。我已经包含了我拥有的所有文件,因为我已经看到它以其他方式完成(所有在一个文件中)并且可以'似乎像这样重现它。
错误1错误C2433:'man':'朋友'不允许进行数据声明 car.h 第14行
错误2错误C4430:缺少类型说明符 - 假定为int。注意:C ++不支持default-int car.h 第14行
错误3错误C2248:'car :: color':无法访问“car”类中声明的私有成员 man.cpp 第13行
错误4错误C2061:语法错误:标识符'car' man.h 第16行
错误5错误C2660:'man :: printCarColor':函数不带1个参数 source.cpp 第16行
错误6错误C2061:语法错误:标识符'car' man.h 第16行
car.h
#pragma once
#include <iostream>
#include <string>
#include "man.h"
using namespace std;
class car
{
public:
car();
car(string);
~car();
friend man;
private:
string color;
};
car.cpp
#include "car.h"
car::car()
{
}
car::car(string c){
color = c;
}
car::~car()
{
}
man.h
#pragma once
#include <iostream>
#include <string>
#include "car.h"
using namespace std;
class man
{
public:
string name;
man();
man(string);
void printCarColor(car);
~man();
};
man.cpp
#include "man.h"
man::man()
{
}
man::man(string newname)
{
name = newname;
}
void man::printCarColor(car mycar){
cout << mycar.color;
}
man::~man()
{
}
source.cpp
#include <iostream>
#include <string>
#include "car.h"
#include "man.h"
using namespace std;
int main()
{
car myCar("green");
//cout << myCar.color;
man myMan("Jim");
myMan.name;
myMan.printCarColor(myCar);
system("pause");
return 0;
}
答案 0 :(得分:0)
您有一个循环包含依赖项。 car.h包括man.h,man.h包括car.h.这不行。删除#include&#34; man.h&#34;从car.h,使用朋友类男人而不是朋友男人。 - n.m。
**希望这不是一个回答这个问题的坏方法,信用转到^