Problem Linking Headers and Source File

Hello, im new to c++ and am trying to link a header and source file to my main source file.

Its simple and ive looked at videos everywhere and read alot forum posts and tried many different things to no avail.

I defined the class Dialogue in Dialogue.h
Set it to cout "\n" in Dialogue.cpp (Just starts a newline in between text)
print out "Hello" then goes to a newline "person" in the main Source File


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// MainSource.cpp

#include "Dialogue.h"
#include <iostream>
#include <string>

using namespace std;



int main()
{
Dialogue Dialogue1;
    cout << "Hello, ";
dialogue1.singlespace();
    cout << "person.";
            
    return 0;
}



Dialogue.cpp Error:
Line 8 Col 1 [Error] prototype for 'int Dialogue::singlespace()' does not match any in class 'Dialogue'
Line 9 Col 1[Error] candidate is: void Dialogue::singlespace()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Dialogue.cpp

#include "Dialogue.h"
#include <iostream>

using namespace std;



void Dialogue::singlespace()
{
	cout "\n";

return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Dialogue.h

#ifndef DIALOGUE_H
#define DIALOGUE_H
#include <iostream>
#include <string>

class Dialogue
{
public:
  void singlespace();

};

#endif 
Last edited on
On line 10 in your Dialogue.cpp you forgot the return type. Also on line 12 you need a ;

In MainSource.cpp you need to create an object of the class Dialogue before you can call a method of the class.
What do you mean I forgot the return type? The only place to put a return is line 14. Im new to C++ could you reiterate please.

Updated code
Last edited on
In Dialogue.h you have void singlespace();

In Dialogue.cpp it needs to be void Dialogue::singlespace()

In main it should be
1
2
3
4
Dialogue Dialogue1;
cout << "Hello, ";
Dialogue1.singlespace();
cout << "person.";
The first error is gone, thanks my dude.

Now I'm getting error in main

Line 13 Dialogue1 was not declared in this scope
What does your main() look like now ?
Updated
C++ is case sensitive so it must be Dialogue1.singlespace(); on line 15
Thanks mustve missed that, more errors thought.
Im starting to think im fucking up terribly bad somewhere if it takes me 12 hours to link a file.

In main:

undefined reference to `Dialogue::singlespace()'
[Error] ld returned 1 exit status


In Dialogue.h
In member function 'void Dialogue::singlespace()':
Line 11 Col 7 [Error] expected ';' before string constant
Line 13 Col 8 [Error] return-statement with a value, in function returning 'void' [-fpermissive]
am trying to link a header and source file to my main source file

For precision sake, what you want to link are MainSource.cpp and Dialogue.cpp.
Dialogue.h will not be linked, but will be included (=copied into) by this directive:
#include "Dialogue.h"

You can try to compare your code to the following, which compiles:
MainSource.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include "Dialogue.h"

using namespace std;

int main()
{
    Dialogue dialogue1;
    cout << "Hello, ";
    dialogue1.singlespace();
    cout << "person.";
    return 0;
}


Dialogue.cpp
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "Dialogue.h"

using namespace std;

void Dialogue::singlespace()
{
    cout << "\n";
}


Dialogue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef DIALOGUE_H
#define DIALOGUE_H

#include <iostream>
#include <string>

class Dialogue
{
public:
    void singlespace();
};

#endif 


In Dialogue.h
In member function 'void Dialogue::singlespace()':
Line 11 Col 7 [Error] expected ';' before string constant
Line 13 Col 8 [Error] return-statement with a value, in function returning 'void' [-fpermissive]

I’m pretty sure the error referred to Dialogue.cpp, not Dialogue.h.
You need “<<” after “cout”:
cout << "\n";
And you can’t return anything from a “void” function:
1
2
3
4
5
void Dialogue::singlespace()
{
    cout << "\n";
    return 0;
}


... if it takes me 12 hours to link a file.

You can safely relax :-)
In 99.99% of courses or tutorial or whatever, C++ is explained in such a conceptual, academic and abstract style that everybody comes across troubles the first time tries to do something by their own. Just press ahead and don’t let these setbacks depress you.
Thank you my dude, Ill go over this when I wake up. Discovered trying to code feeling like a zombie causes many stupid issues. Again thanks everyone for the replies Ill check back tomorrow.
The function is putting out a newline character - if it really should be a single space, it can change to cout << " "
Enoizat tried your code and it wont compile for me strange error.

Line 18
C:\crossdev\src\mingw-w64-v3-git\mingw-w64-crt\crt\crt0_c.c undefined reference to `WinMain'

returning 1 exit status also. Using DevC++ IDE.



wildblue I know what it does dw about it my man.
Line 18
C:\crossdev\src\mingw-w64-v3-git\mingw-w64-crt\crt\crt0_c.c undefined reference to `WinMain'

Line 18? There isn't the 18th line in any file :-) :-)
What are you compiling?
1) about Dev-C++:
http://www.cplusplus.com/articles/36vU7k9E/

2) We can try to compile directly from a DOS console, if your MinGw version is correctly installed (i.e. it appears in the path).
To discover this, please open a DOS console and type:
g++ --version
followed by ENTER.
If the system answers with a meaningful statement, like (more or less):
g++ (GCC) 7.1.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

we can go on.

You just need to enter the folder where are you files and type:
g++ MainSource.cpp Dialogue.cpp -o dialogue.exe
The program should compile. Then, to execute it, just type:
dialogue.exe
and press ENTER.

Last edited on
> undefined reference to `WinMain'
chose `Console Project'
Topic archived. No new replies allowed.