簡體   English   中英

使用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

這是我的代碼,其中包含主文件,標頭文件和源文件,我正在嘗試從類AddressSpace中打印出訪問器函數,但其​​說法是

對“ 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;
}

AddressSpace.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

AddressSpace.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;
}

main()的以下代碼行是函數聲明或原型。

AddressSpace ob(string address,string town,string state,int postal);

如果刪除括號內的類型名稱,則將使用給定的參數來構造一個名為ob的對象。

AddressSpace ob(address, town, state, postal);

在聲明一個新對象時,您不要在類型前加上前綴。 您也沒有實例化字符串或整數。 在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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM