0

I know it may seem like this has been asked before but I've looked around and the static method didn't work for me. Here's my code:

struct Customer {
public:
    string get_name();
private:
    string customer,first, last;
};

Here's where I call the function:

void creation::new_account() {
Customer::get_name(); //line it gives the error on.
}

Here's an example of some code that compiles fine.

struct Creation { public: string get_date(); private: string date; };

then I call it the same way

void Creation::new_account() { Creation::get_date();}

Hence my confusion why one works and the other doesn't.

EDIT: Ok I get it, I just realized I was calling a function of another struct inside a function definition that's part of a different class. I got it, thanks to all who answered

4
  • It's not static, you should call it through an object. Commented Oct 2, 2013 at 7:10
  • As pointed out, you missed out on declaring the function as static and hence the error. Can you put up the original code where "static" did not work for you? Commented Oct 2, 2013 at 7:25
  • @NotAgain sure it was this 'struct Customer { public: static string get_name(); private: string customer,first, last; };' Commented Oct 2, 2013 at 7:29
  • Let me guess. You wrote a static function get_name() and tried to return the contents of string customer,first or last. That would not work because static member functions have access to static data of the class only. And none of the three string data members are static here. Commented Oct 2, 2013 at 10:36

3 Answers 3

1

It is not declared static (needs to be static std::string get_name();). However, get_name() for Customer is a specific attribute of a Customer instance so having it static does not make sense, that is the same name for all instances of Customer. Declare an object of Customer and use it. It would make sense to have the name provided to the constructor of Customer, as surely a customer cannot exist without a name:

class Customer {
public:
    Customer(std::string a_first_name,
             std::string a_last_name) : first_name_(std::move(a_first_name)),
                                        last_name_(std::move(a_last_name)) {}
    std::string get_name();
private:
    std::string first_name_;
    std::string last_name_;
};

Declare an instance of Customer:

Customer c("stack", "overflow");
std::cout << c.get_name() << "\n";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was trying to avoid passing an object through, but I suppose an empty object will work the same for my purposes.
0

Since your get_name is not declared static, it is a member function.

You probably need some constructors in your Customer class. Assuming you have some, you could code

 Customer cust1("foo123","John","Doe");
 string name1 = cust1.get_name();

You need an object (here cust1) to call its get_name member function (or method).

Time to spend many hours reading a good C++ programming book.

Comments

0

"The static method didn't work for me". It's not a method it's how the language works.

If you want to call some method without a concrete object, you need it to be static. Otherwise, you need an object.

Your code will work with one of the following :

struct Customer {
public:
    static string get_name();
private:
    string customer,first, last;
};

or

void creation::new_account() {
    Customer c;
    //stuff
    c.get_name();
}

2 Comments

Sorry I typed method, but semantics aside, what I mean by that is I typed static string get_name(); and the compiler complained. But thanks the second worked. The reason I'm confused is because I have two other functions defined and declared the same and I created another just to be sure and without that function it compiled fine.
@Shilaly If the compiler complained when you actually used static string get_name();, then you made another error somewhere else...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.