2

I've looked at a couple other questions asking this, but mine seems to be a lot simpler of a case then the ones I've been through, so I'll ask my case for this.

Learn.h:

#ifndef LEARN_H
#define LEARN_H

class Learn
{
public:
    Learn(int x);
    ~Learn();

private:
    const int favourite;
};

#endif

Learn.cpp:

#include "Learn.h"
#include <iostream>
using namespace std;

Learn::Learn(int x=0): favourite(x)
{
    cout << "Constructor" << endl;
}

Learn::~Learn()
{
    cout << "Destructor" << endl;
}

Source.cpp:

#include <iostream>
#include "Learn.h"
using namespace std;

int main() {
    cout << "What's your favourite integer? "; 
    int x; cin >> x;
    Learn(0);

    system("PAUSE");
}

The above code in itself does not output any error.

However, I do get a couple errors after I replace Learn(0) with Learn(x). They are:

  • Error E0291: no default constructor exists for class Learn
  • Error C2371: 'x' : redefinition; different basic types
  • Error C2512: 'Learn' : no appropriate default constructor available

Any reason for this? I really want to actually input the integer variable x inside it rather than the 0. I know this is only practice and I'm new to this, but really, I'm a little confused as to why this doesn't work.

Any help would be appreciated, thanks.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Osama Kawish
  • 312
  • 1
  • 5
  • 15
  • 1
    You tried to specify the default value of x (in the Learn ctor) in the .cpp. You should instead define it in the header. – ZeroUltimax Sep 27 '17 at 17:48
  • @ZeroUltimax is correct about the default parameter, but the real reason the compiler is complaining is that it thinks you're trying to define a function named `Learn`. You can't call a constructor quite like you're trying to. You need to use `Learn some_name(x);`. – Jonesinator Sep 27 '17 at 17:50
  • [OT]: `Learn::Learn(int x=0)` is useless in your cpp, as the default value is only available in that cpp file. Remove it, or place it in your header. – Jarod42 Sep 27 '17 at 17:58

3 Answers3

8

Parsing issue:

Learn(x);

is parsed as

Learn x;

You should use

Learn{x};

to build your temporary or

Learn some_name{x};
//or
Learn some_name(x);

if you want an actual object.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 1
    Maybe better give it a name: `Learn tmp{x};` – rustyx Sep 27 '17 at 17:58
  • 2
    @RustyX: That is no longer a temporary, and the print of `"Destructor"` will happen after the pause. Possibly using extra scope `{Learn tmp{x};}` if you want absolutely name it. – Jarod42 Sep 27 '17 at 18:01
  • @Jarod42 What's a temporary? Sorry, a little new to C++. Also, bit confused as to why the change in brackets made a difference here. – Osama Kawish Sep 27 '17 at 22:24
0

Okay, I figured out the problem I was having. I didn't realize that the call is done as part of an object assignment. The notation in C++ seems to be a bit different that way.

So Learn(x) should be replaced with Learn obj(x) apparently.

This notation is a little off from other programming languages where you can usually write className(inputs) variableName.

Osama Kawish
  • 312
  • 1
  • 5
  • 15
0

I had similar code and came up with a different errors because I was compiling with Linux in a terminal. My errors were:

error: conflicting declaration 'myObject str' 
error: 'str' has a previous declaration as 'const string  str'

when I tried myObject(str);

Sure Learn{x}; (and in my case myObject{str};) will successfully compile, but it creates a temporary object, then destroys it in the same line.

This is not what I wanted to do. So an alternative solution is to create a new object like:

Learn var_name = new Learn(x);

This way you can reference it for future use.

Jake
  • 617
  • 1
  • 6
  • 21