Hello everyone! I have been writing Java for quite some time now and I though I'd try C++. So I have a problem I can't quite figure out.
The code I tried to write is simply calling a function from another class (located in another file) to print a simple message. The following is the code:
The main class file
The "class the function is in"'s header file (Printer.h):
The second class (Printer.cc):
This code runs perfectly fine on Windows (using Microsoft Visual C++ 2010 Express) but on my Mac i get the following error:
Now the akward part. On my Mac if I change the following line in the main file
to
everything runs smoothly (on Windows it did not compile, as I expected). The thing is everywhere I looked I'm supposed to include the header file in the main class not the .cc file (Changing the extension was just sheer coincidence). I'm pretty sure including the .cc is the wrong way to go.
Any help on why this is happening would be greatly appreciated.
Edit: Code compiled with g++
The code I tried to write is simply calling a function from another class (located in another file) to print a simple message. The following is the code:
The main class file
Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include "Printer.h"
using namespace std;
//inline void keep_window_open() {char ch; cin>>ch;}
int main(){
Printer printer;
printer.actualPrint();
return 0;
}
The "class the function is in"'s header file (Printer.h):
Code:
#include <iostream>
using namespace std;
class Printer{
public:
//Declaration of the printing function
void actualPrint();
};
The second class (Printer.cc):
Code:
#include "Printer.h"
void Printer::actualPrint(){
std::cout<<"Hooray!! Printing from another class!!!\n";
}
This code runs perfectly fine on Windows (using Microsoft Visual C++ 2010 Express) but on my Mac i get the following error:
Code:
Undefined symbols:
"Printer::actualPrint()", referenced from:
_main in ccnldy3J.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Now the akward part. On my Mac if I change the following line in the main file
Code:
#include "Printer.h"
to
Code:
#include "Printer.cc"
everything runs smoothly (on Windows it did not compile, as I expected). The thing is everywhere I looked I'm supposed to include the header file in the main class not the .cc file (Changing the extension was just sheer coincidence). I'm pretty sure including the .cc is the wrong way to go.
Any help on why this is happening would be greatly appreciated.
Edit: Code compiled with g++
Code:
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5666.3~6/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)
Last edited: