C++重复了类类型的定义
C++ repeated definitions of class types?
下午好!
如果这个问题看起来很模糊,很抱歉,但这里有一些(不完整的)上下文代码。具体来说,这是关于"UserInfo-inputInfo"定义部分的,如实现文件中的函数UserInfo::setUserInfo()和UserInfo::displayProfile()中所示。
project02.cpp(实现文件)
#include <iostream>
#include "project02.h"
using namespace std;
void UserInfo::setUserInfo()
{
UserInfo inputInfo;
string fName;
string lName;
int bYear;
string city;
string occupation;
cout << "Please enter your first name: ";
cin >> fName;
inputInfo.setFirstName(fName);
cout << "Please enter your last name: ";
cin >> lName;
inputInfo.setLastName(lName);
cout << "You are now registered as: " << inputInfo.getFirstName() << " " << inputInfo.getLastName();
}
void UserInfo::displayProfile()
{
UserInfo inputInfo;
cout << "Profile Information:" << endl;
cout << "Name: " << inputInfo.getFirstName() << " " << inputInfo.getLastName();
}
void UserInfo::setFirstName(string fName)
{
_firstName = fName;
}
string UserInfo::getFirstName()
{
return _firstName;
}
void UserInfo::setLastName(string lName)
{
_lastName = lName;
}
string UserInfo::getLastName()
{
return _lastName;
}
project02.h(头文件)
#ifndef PROJECT02_H
#define PROJECT02_H
using namespace std;
class UserInfo
{
public:
string getFirstName();
void setFirstName(string first);
string getLastName();
void setLastName(string last);
int getBirthYear();
void setBirthYear(int year);
string getCurrentCity();
void setCurrentCity(string city);
string getOccupation();
void setOccupation(string occ);
void setUserInfo();
void displayProfile();
private:
string _firstName;
string _lastName;
int _birthYear;
string _currentCity;
string _occupation;
};
#endif // PROJECT02_H
project02main.cpp(主文件)
#include <iostream>
#include "project02.h"
using namespace std;
int main()
{
UserInfo inputInfo;
inputInfo.setUserInfo();
return 0;
}
现在的问题是:除了在实现文件中每次为不同的函数重复定义对象"UserInfo inputInfo;"之外,还有其他选择吗?
此时不要在该数据类型的方法中创建相同数据类型的对象-只需调用setFirstname()和getFirstname。
相关文章:
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 列表参数的类型定义
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 关于 C++ 中的函数类型定义
- C++(和 ROS) - 包含与前向声明引用,设置默认值和类型定义
- 将使用/类型定义限制为类范围
- 模板类型定义?
- C++:模板类的类型定义
- 如何对命名空间限定类型进行类型定义?
- 此递归模板类型定义是否有效C++?
- 具有调整对齐方式的类型定义
- C++从抽象类型定义类成员
- 用于C++代码的 API 监视器类型定义 (XML)
- 如何将result_of与函数类型定义一起使用
- 在C++的适当类型定义位置
- 如何根据模板类型定义浮点常量?
- 如何为缺少预定义运算符而不扩展命名空间"std"的标准类型定义运算符>> (istream &, ...)?
- 参数化类的别名(或类型定义)内部类
- 如果我想从类型"T"定义元素的容器(来自 STL),那么"T"必须使用默认构造函数?