I'm trying to write a basic program using class/function header files and can't seem to get this to work/link together.
It might just be the order that I'm doing my #includes and such in, but the two errors that I'm getting right now are:
: error C2871: 'std' : a namespace with this name does not exist
: error C2065: 'cout' : undeclared identifier
This is what my code looks like:
main.cpp
1 2 3 4 5 6 7 8 9 10 11
|
#include <iostream>
#include "header.h"
int main(){
Stuff myStuff;
myStuff.setData( 5 );
myStuff.printData();
return 0;
}
|
header.h
1 2 3 4 5 6 7 8 9 10
|
#pragma once
#define HEADER_H
using namespace std;
class Stuff{
int data;
public:
void setData(int);
void printData() {cout << data;}
};
|
header.cpp
1 2 3 4 5
|
#include "header.h"
void Stuff::setData (int x){
data = x;
}
|
Even more odd to me was that when I wrote the same program on my school's linux server and compiled with g++, I got a different looking error:
$ g++ main.cpp
/tmp/cca5fvnX.o: In function `main':
main.cpp:(.text+0x19): undefined reference to `Stuff::setData(int)'
collect2: ld returned 1 exit status
I google'd around a bit and didn't find the answer to my problem. I've been kind of trying to teach myself how to do C++ with what I read online, have I read something wrong?