1

I'm new to c++, I run the following code in visual studio c++

    struct bob
    {
       double a,b;      
       bob(double a,double b);
    }

    int main()
    {
        bob z(2.2,5.6);
        cout<<z.a<<endl;
        keep_window_open();
        return 0;
    }     

when I run this code, i get the following error:

Error 1 error LNK2019: unresolved external symbol "public: __thiscall bob::bob(double,double)" (??0bob@@QAE@NN@Z) referenced in function _main C:\drives\Comp-Mech\programming\V.S\C++\projects\E1\E1.obj E1

11
  • You only declare bob(double a,double b);, where is your implementation? Commented May 1, 2013 at 12:35
  • feature request for C++17: automatic constructor generation from its declaration when written as bob(double, double) = default; and signature matches data members. Commented May 1, 2013 at 12:36
  • @rhalbersma: Why? Why not just drop the constructor altogether and use aggregate initialization? Commented May 1, 2013 at 12:40
  • @DavidRodríguez-dribeas that would not work for private class data members Commented May 1, 2013 at 12:43
  • 1
    @rhalbersma: Yes, but the request for the feature and where issues can crop are separate places, which makes this as dangerous as aggregate initialization. Say that you have a type struct X { char ch, int a; double d; }; for which you use this feature and you call X x(65,32,3) somewhere else in the code. Now someone notices that you waste space and reorders the members: struct X { double d; int a; char ch; };. User code that expected x.ch == 'A'; now sees x.ch == ' ' (assuming ASCII). If you provided the constructor yourself it wouldn't matter, all initialization is named Commented May 1, 2013 at 14:15

3 Answers 3

5

You need to implement constructor of your class bob:

 bob::bob(double a,double b) : a(a), b(b) {}
Sign up to request clarification or add additional context in comments.

4 Comments

There's a colon missing though.
@piokuc Interesting, so the class members are not visible inside the parens. Or is it that the params shadow them?
It's not that they are not visible, but they are shadowed by the constructor's parameters. It does work, believe me, or test it ;)
@piokuc, Yes that should be obvious. Not sure why I had that wrong.
3

You have provided a declaration for bob's constructor, but you have not given a definition. The definition gives the implementation of the constructor and says exactly what it should do. In this case, you likely want your constructor to assign its arguments to the object's member variables:

bob::bob(double a, double b)
{
  this->a = a;
  this->b = b;
}

I used assignment in the above code because you are more likely to be familiar with it. However, you should be aware of member initialization lists which allow you to initialize members directly:

bob::bob(double a, double b)
  : a(a), b(b)
{ }

This says to initialise the member a with the argument a and initialise member b with the argument b. It also avoids potentially expensive default initialization of members before assigning to them.

5 Comments

This is a struct, so no need for explicit public
@piokuc Noticed that immediately after saying it.
you might mention that for user-defined types with default constructors, the member initializer is also more efficient since it avoid double initialization
I won't, although I feel like downvoting for proposing assignments and defending it with a To keep it simple. Initialization lists are just as simple if people see and use them from the beginning, it is this kind of simpler if doing assingment that ends with tutorials teaching bad practice before telling people what the right thing to do is.
@DavidRodríguez-dribeas Reworded. Didn't mean to suggest it was simpler to do that, just simpler to understand for the asker.
1

That's because you haven't written the code for bob::bob(double, double).

struct bob
{
   double a,b;      
   bob(double aa, double bb) a(aa), b(bb) {}
};

Comments

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.