使用 c++ 的代码块我试图从我的类中打印出一个访问器函数。错误表明对成员 get_address 的请求
Posted
技术标签:
【中文标题】使用 c++ 的代码块我试图从我的类中打印出一个访问器函数。错误表明对成员 get_address 的请求【英文标题】:using code blocks for c++ I'm trying to print out an accessor function from my class. the error states that request for member get_address 【发布时间】:2015-01-23 00:52:36 【问题描述】:这是我的代码,它包括主文件、头文件和源文件
请求“ob”中的成员“get_address”,它是非类类型 AddressSpace(std::string, std::string, std::string, int) aka AddressSpace(std::basic_string, std ::basic_string, std::basic_string, int)
main.cpp
#include <iostream>
#include <string>
#include "AddressSpace.h"
#include "AddressSpace.cpp"
using namespace std;
int main()
string address;
string town;
string state;
int postal;
cout << "What is the street you live on?: " <<endl;
cin >> address;
cout << "What is the city you live in?: " <<endl;
cin >> town;
cout <<"What is the state you live in?: " << endl;
cin >> state;
cout << "What is the postal code?: " << endl;
cin >> postal;
AddressSpace ob(string address,string town,string state,int postal);
cout << "Address" << ob.get_address() << endl;
return 0;
地址空间.h
#ifndef ADDRESSSPACE_H_INCLUDED
#define ADDRESSSPACE_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
class AddressSpace
public:
//Default constructor
AddressSpace();
//overload constructor
AddressSpace(string, string, string, int);
//Accessor Functions
string get_address() const;
// get_address - returns address
string get_town() const;
// get_town -returns town
string get_state() const;
// get_state - returns state
int get_postal() const;
// get_postal returns zip code
private:
//member variables
string street;
string city;
string st;
int zip;
;
#endif // ADDRESSSPACE_H_INCLUDED
地址空间.cpp
#include "AddressSpace.h"
AddressSpace::AddressSpace(string address, string town, string state, int postal)
string street = address;
string city = town;
string st = state;
int zip = postal;
string AddressSpace::get_address() const
return street;
string AddressSpace::get_town() const
return city;
string AddressSpace::get_state() const
return st;
int AddressSpace::get_postal() const
return zip;
【问题讨论】:
【参考方案1】:main()
中的以下代码行是function declaration 或原型。
AddressSpace ob(string address,string town,string state,int postal);
如果您删除括号内的类型名称,它将按照您的意图使用给定的参数构造一个名为 ob
的对象。
AddressSpace ob(address, town, state, postal);
【讨论】:
感谢它遵守 :)【参考方案2】:当声明一个新对象时,你不要在它前面加上类型。您还没有实例化字符串或整数。在 C/C++ 中,未实例化的值是随机的,因此最好给每个变量一个默认值。您还应该包含 AddressSpace.h 和 AddressSpace.cpp 的代码,以便我们更好地了解可能出现的其他问题
另外,请注意 main() 参数的变化。没有必要,但始终包含它被认为是一种很好的形式。如果您的程序是从命令行运行的,这些参数将填充参数的数量和参数本身。
我认为您的代码应该看起来更像这样:
#include <iostream>
#include <string>
#include "AddressSpace.h"
//#include "AddressSpace.cpp" //This line isn't needed, you should only include the header file
using namespace std;
int main(int argc, char** argv)
string address = "";
string town = "";
string state = "";
int postal = 0; //Note postal codes for Europe include letters, and you most-likely won't be doing math with a zipcode so it might make more sense to make it a string
cout << "What is the street you live on?:" << endl;
cin >> address;
cout << "What is the city you live in?:" << endl;
cin >> town;
cout << "What is the state you live in?:" << endl;
cin >> state;
cout << "What is the postal code?:" << endl;
cin >> postal;
AddressSpace ob(address, town, state, postal);
cout << "Address " << ob.get_address(); << endl;
return 0;
【讨论】:
感谢您的帮助和正确编程的建议。我确实包含了头文件和源文件,我不知道为什么你看不到它们以上是关于使用 c++ 的代码块我试图从我的类中打印出一个访问器函数。错误表明对成员 get_address 的请求的主要内容,如果未能解决你的问题,请参考以下文章
我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情