简体   繁体   中英

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

This is my code it includes the main, header and source file I'm trying to print out an accessor function from my class AddressSpace, but its saying that

request for member "get_address" in "ob", which is of non-class type 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;
}

The following line of code in main() is a function declaration or prototype.

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

If you remove the type names inside the parentheses it will do what you intended, to construct an object named ob using the given parameters.

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

When declaring a new object you don't prefix it with the type. You also haven't instantiated the strings or integer. In C/C++ un-instantiated values are random, so it's good practice to give every variable a default value. You should also include the code for AddressSpace.h and AddressSpace.cpp so we have a better idea of what else could be going wrong

Also, notice the change to the parameters of main(). It's not necessary, but considered good form to always include it. These arguments are filled in with the number of arguments and the arguments themselves if your program is run from the command line.

Your code should look more like this I think:

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM