C++ 如何在将类拆分为.h和.cpp文件时使用聚合? 上下文
我的教授给了我一个任务,让我使用两个类之间的聚合来制作一个程序,同时将这些类划分为C++ 如何在将类拆分为.h和.cpp文件时使用聚合? 上下文,c++,class,c++11,aggregation,C++,Class,C++11,Aggregation,我的教授给了我一个任务,让我使用两个类之间的聚合来制作一个程序,同时将这些类划分为.h和.cpp文件 我的解决方案 包含类声明的头文件: #include <iostream> #include <string> using namespace std; class medicalCompany { private: string ceoName; string email; string phoneNumber; string loca
.h
和.cpp
文件
我的解决方案
包含类声明的头文件:
#include <iostream>
#include <string>
using namespace std;
class medicalCompany {
private:
string ceoName;
string email;
string phoneNumber;
string locate;
public:
medicalCompany();
void Name(string n);
void mail(string m);
void phone(string p);
void location(string l);
~medicalCompany();
};
class origin {
private:
medicalCompany country;
public:
origin();
void address();
~origin();
};
问题
我遇到了以下错误:
Severity Code Description Project File Line Suppression State
Error C3867 'medicalCompany::location': non-standard syntax; use '&' to create a pointer to member CP2_HW c:\function.cpp 41
您可以:
- 替换
void origin::address(){cout
cout我确信错误消息与我所说的有关。这里的这行:
cout yes,因为locate是一个私有成员。您可以替换
void origin::address(){cout Make
一个公共成员。或者在locate
中添加一个公共函数来返回它。@drescherjm这个问题是由arnaud先生写的那句话解决的,问题和你说的一模一样,drescherjm所以非常感谢大家的帮助^ ^它起作用了,先生谢谢你的帮助和伟大的建议,最好交换名字谢谢,欢迎!C++有时会让人困惑,尤其是当你开始时,@德雷斯切姆也有解决的办法!medicalCompany
#include <iostream> #include <string> #include "function.h" using namespace std; int main() { medicalCompany o; o.Name("jack"); o.mail("ouremail@company.com"); o.phone("2342352134"); o.location("Germany"); origin o2; return 0; }
Severity Code Description Project File Line Suppression State Error C3867 'medicalCompany::location': non-standard syntax; use '&' to create a pointer to member CP2_HW c:\function.cpp 41
#include <iostream> #include <string> class medicalCompany { private: std::string _ceoName; std::string _email; std::string _phoneNumber; std::string _location; public: // Constructor medicalCompany(std::string name, std::string email, std::string phone, std::string location): _ceoName(name), _email(email), _phoneNumber(phone), _location(location) {} friend ostream& operator<<(ostream& os, const medicalCompany& dt); };
ostream& operator<<(ostream& os, const medicalCompany& co) { os << co._ceoName << " " co._phoneNumber << ' ' << co._email; return os; }
int main() { medicalCompany o("jack", "ouremail@company.com","2342352134","Germany") std::cout << o << std::endl; return 0; }