Whats wrong with this?

I made this to try to learn how to use multiple source files. It's in code:blocks, and when I build it, I get no errors or warnings. When I run it just opens up the console and nothing is there. I'm just trying to print out "Hello!" on the screen.

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

using namespace std;

int main()
{
void hello();
return 0;
}


My main file^

1
2
3
4
5
6
7
8
9
10

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

void hello()
{
    cout << "Hello!" << endl;
    return;
}


Implementation file^

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#ifndef MULT_H_INCLUDED
#define MULT_H_INCLUDED

#include<iostream>

using namespace std;

void hello();



#endif // MULT_H_INCLUDED


Header file^ titled mult.h
In main, you're not calling hello(), you're simply re-declaring it.

1
2
3
4
5
6
int main()
{
   void hello(); // <-- this is declaring hello()
   hello(); // this should work for you
   return 0;
}
Code:blocks says


obj\Debug\main.o||In function `main':|
C:\Users\Austin\Desktop\Hello_World\Bla\main.cpp|8|undefined reference to `hello()'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|

Did you copy my code exactly? I didn't mean for that. You still need to #include "mult.h"
Last edited on
No I just made the change that you did in line 4.
MSVC gives an error also.
Last edited on
I made the following three files with this code verbatim:

1
2
3
4
5
6
7
8
9
10
11
12
//Main.cpp

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

using namespace std;

int main()
{
hello();
return 0;
}


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

#ifndef MULT_H_INCLUDED
#define MULT_H_INCLUDED

#include<iostream>

using namespace std;

void hello();



#endif // MULT_H_INCLUDED 


1
2
3
4
5
6
7
8
9
10
//mult.cpp

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

void hello()
{
    cout << "Hello!" << endl;
    return;
}


and it built and ran fine in Visual Studio (2012)
Copy and pasted and it didn't work in either Code:blocks are Visual Studio 2010.
There's nothing syntactically wrong with this code. There must be something wrong with your project setup.

Make sure that your main and implementation files can see mult.h either through proper setup of include directories or have all files in the same directory. I don't know if this is the problem though because it would've shown up as a compilation error. Also make sure that all three files are added to the project. You could also try cleaning / rebuilding the project.

If all else fails, try creating a new project from scratch following the steps given here: http://wiki.codeblocks.org/index.php?title=Creating_a_new_project
It has to be something like that. I checked the book and it said to use the exact same syntax as you did,
It looks like you meant for multi to be a class. If so declare the multi class in multi.h, make sure it has hello as a public function. In main create an object of type multi, and use it to run the hello function.

I am suggesting this because it won't hurt to learn a trivial class like this - seen as as you would like to learn C++.

Look in the information / reference / articles section at the top left of this page for heaps of info, especially the tutorial article.

HTH
How did you create the files? Did you go up to Project->Add New Item-> New .cpp/.h wizard?
or did you go to file->New File...
Some compiler versions prefer the first way of adding files into your project, others don't care either way. (just make sure the files are saved into your project folder)

The other way to add files to your project is to locate your project folder and copy/paste directly into that folder.

Are you getting any error messages in your debugger? Post those too so we can take a look.
Last edited on
Herrgud do I feel stupid. Works now guys. Sorry, I guess I should have read the codeblocks manual after all. Sorry, I feel like I just wasted an hour of everyones time. I'll properly add files every time now.
Austin J wrote:
I feel like I just wasted an hour of everyones time.
Don't worry - someone will find this on google some day and they'll realize they made the same mistake as you ;)
Move using namespace std; from mult.h to mult.cpp and this will be a job well done. ;)
Topic archived. No new replies allowed.