Creating header files and includes

Jan 28, 2017 at 4:10am
I'm attempting to create a function called Greetings, where I put it in a separate file. I followed a tutorial on youtube, but it kept giving me errors. Below is the source code for the main.cc, Greetings.cc, and Greetings.h. Though when I tried compiling it through my Mac's terminal it ran quite fine, but when I tried running it inside my text editor (Sublime Text 3), it keeps on giving me these weird errors, long ones. Does anyone see anything wrong?

main.cc
1
2
3
4
5
6
7
8
#include <cstdio>
#include "Greetings.h"
using namespace std; 

int main() {
 Greetings bo;
 return 0;
}


Greetings.cc
1
2
3
4
5
6
#include <cstdio>
#include "Greetings.h"

Greetings::Greetings() {
 printf("Hello, how are you?\n");
}


Greetings.h
1
2
3
4
5
6
7
8
9
#ifndef GREETINGS_H 
#define GREETINGS_H 

class Greetings {
public:
 Greetings();
};

#endif 
Last edited on Jan 28, 2017 at 4:12am
Jan 28, 2017 at 5:12am
Your program is a mix of C (printf) and C++(<cstdio>), etc. First decide which language you're going to use. For C++:

main.cc
1
2
3
4
5
6
7
8
#include <iostream>//this is the header file you need
#include "Greetings.h"
using namespace std; //AVOID

int main() {
 Greetings bo;
 return 0;
}


Greetings.cc
1
2
3
4
5
6
#include <iostream>
#include "Greetings.h"

Greetings::Greetings() {
 std::cout << "Hello, how are you?  \n";
}


Greetings.h - unchanged

Jan 28, 2017 at 5:36am
Alright so in order to use printf, I use #include <cstdio>, which is a C++ language, and in order to use cout, I use #include <iostream>, which is a C language?

Also, made the correction based off of your feedback. It still gave me errors. Here they are.
Undefined symbols for architecture x86_64:
"Greetings::Greetings()", referenced from:
_main in main-e356ad.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 3.4s with exit code 1]
[shell_cmd: g++ "/Users/conrados/Desktop/C++ Programs/Greetings/main.cc" -o "/Users/conrados/Desktop/C++ Programs/Greetings/main" && "/Users/conrados/Desktop/C++ Programs/Greetings/main"]
[dir: /Users/conrados/Desktop/C++ Programs/Greetings]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
Jan 28, 2017 at 10:02am
I suggest you google, at least, the following terms and then come back here if something is still unclear: iostream, cstdio, printf, std::cout, "differences between C and C++"
Re your error messages make sure (a) all your files are in the same folder, and (b) command line is run from that folder - at least that's how it works in Windows. I just re-tried with the following:
g++ -Wall -o test implement.cpp main.cpp
i.e. running the complier (g++) with all common warnings on (-Wall) to create an executable (test) using two source files (implement.cpp, main.cpp) and at the next command prompt typing in
test
i.e. the name of the executable file gave desired output
Jan 28, 2017 at 11:49pm
It works now. Thanks.
Topic archived. No new replies allowed.