0

I just learned about C++ classes in multiple files, but I cannot get the output I expect from it...

(main.cpp)

#include <iostream>
#include "Dog.h" //include to be able to use the class

using namespace std;

int main(){
    Dog myDoggy();
    cout << "here";

    return 0;
}

(Dog.cpp)

#include "Dog.h" // includes the header file
#include <iostream> 

using namespace std;

Dog::Dog(){  //class_it_belongs_to::function
    cout << "Woof" << endl; //output I want to receive
}

(Dog.h)

// This is a C++ header file
#ifndef DOG_H
#define DOG_H 
// You define everything here before you use it

class Dog{
    public:
    Dog();  
};

#endif
// The reason for this file is because when you compile it, this will be showed to whoever wants to use this piece of code, whilst the other file will be turned into binary (so they cannot edit the bodies of the functions)

This is the output (I expect to see "woof (\n) here" but I only get "here": The output I get after using g++ to compile the code

I use Linux Mint 19.3 and g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0.

Netsu
  • 17
  • 4
  • 1
    `Dog myDoggy();` declares a function taking no parameters and returning `Dog`. It is not a variable definition. Drop parens, as in `Dog myDoggy;` – Igor Tandetnik Jun 20 '20 at 22:10
  • Thank you! My IDE constantly gave me an red line whenever I removed the "()", so I thought it had to be there, thank you so much! – Netsu Jun 20 '20 at 22:13
  • 1
    Unrelated: The fact that your did _not_ do `using namespace std;` in your header file deserves some credit. If you remove it from your `.cpp` files too it'll be grand. – Ted Lyngmo Jun 20 '20 at 22:15

2 Answers2

1

The following rule apply for ambiguity between variables and functions: If it looks like a function, it is a function.
Based on: C++ reference

In case of ambiguity between a variable declaration using the direct-initialization syntax and a function declaration, the compiler always chooses function declaration; see direct-initialization.

Your "call":

Dog myDoggy();

Looks like a function named "myDoggy" that returns a Dog instance and accepts no parameters. This situation happens when you try to construct an object with a default constructor, and insist to mention the empty parenthesis.

There are several ways to create an instance of the class:

Dog myDoggy; // The easiest way.

Dog myDoggy({}); // Only if the constructor is not `explicit`.

Dog (myDoggy); // Same as the first one.

Dog (myDoggy)({}); // Same as the second one.

All the above will generate the same result, remember them so it won't confuse you in the future. There are some bugs that their cause is the misunderstanding of the above ways to declare an instance.

Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
  • "_The strict rule says_ " - With that type of reference, a link to the rule referenced would be a plus. – Ted Lyngmo Jun 20 '20 at 23:15
  • @TedLyngmo you are right, I linked a reference. I remembered it more strictly, but I couldn't find the source of the exact sentence. I replaced it with the most closest thing that I found. – Coral Kashri Jun 20 '20 at 23:27
  • Better, yet what you link to does _not_ say "_If it looks like a function, it is a function_". – Ted Lyngmo Jun 20 '20 at 23:31
  • @TedLyngmo "The following rule apply for ambiguity between variables and functions". – Coral Kashri Jun 20 '20 at 23:33
  • Indeed! I was just made to look like that the "_If it looks like a function ... " claim was based on what you linked to at cppreference. – Ted Lyngmo Jun 20 '20 at 23:35
  • @TedLyngmo look here: https://stackoverflow.com/a/620149/8038186 I am pretty sure that there is a reference somewhere for the base sentence, and I also pretty sure that it is a base rule. – Coral Kashri Jun 20 '20 at 23:38
  • "_I am pretty sure that there is a reference..._:" - Ok, please link directly to that. – Ted Lyngmo Jun 20 '20 at 23:43
0

When you create an instance of a class without params, you need to declare without parenthesis.

Dog myDoggy;