-2

I'm having an undefined reference error in my main. Not sure what I'm doing wrong and have tried changing names and moving things around but keep getting the same error. I'm wondering if maybe it's my IDE but really don't know Here's the code:

#include <iostream>
#include "f.h"
#include "g.h"

using namespace std;

int main()
{
    F f;
    G g;

    f.f();
    g.g();

    return 0;
}

next file:

#ifndef F_H_INCLUDED
#define F_H_INCLUDED

class F
{
public:
    void f();
};

#endif

next file:

#ifndef G_H_INCLUDED
#define G_H_INCLUDED

class G
{
public:
    void g();
};

#endif

next file:

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

void F::f()
{
    std::cout << "This was function f!" << std::endl;
}

next file:

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

void G::g()
{
    std::cout << "This was function g!" << std::endl;
}

edit: so i changed the include from "f.h" and "g.h" to "f.cpp" and "g.cpp" and now it works... can anyone explain why?

cppfan
  • 7
  • 5
  • 2
    make sure you are actually compiling and linking the 3 files that contain functions – M.M Apr 02 '15 at 02:29
  • 2
    What's the full error message? – teppic Apr 02 '15 at 02:32
  • undefined reference to 'F::f()' – cppfan Apr 02 '15 at 02:33
  • undefined reference to 'G::g()' – cppfan Apr 02 '15 at 02:34
  • my guess is that you havent included the f and g.cpp files in your ide solution/project so they didnt get compiled, on which the linker wasnt able to find them and reported it cant find the function definition, or some other similar build issue... or there is a typo I dont see :) (i.e. what Matt said I see now) – Emile Vrijdags Apr 02 '15 at 03:01

2 Answers2

0

It's looks fine, but make sure if your files names are correct, there is difference between lower and upper case.

#include "f.h"
#include "g.h"

its not the same as

#include "F.h"
#include "F.h"
0

I test it with VS2013, it is right, if you copy code, please check it.

thinkerou
  • 1,781
  • 5
  • 17
  • 28