Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
I include the header file, it says cannot find the function definition,
but when I include the cpp file, it works,
why...
 
in A.h
 
#ifndef A_H
#define A_H
 
#include <stdlib.h>

class A{
  int member1;
 public:
  void Assign(int v);
  void Print();
};
 
#endif
 
 
in A.cpp
#include "A.h"
#include <iostream>

using namespace std;
 
void A::Assign(int v){
  member1 = v;
}
void A::Print(){
  cout<<member1<<endl;
}
 
in main.cpp
 
#include <stdlib.h>
#include "A.h";

int main(){
  A a1;
  a1.Assign(1);
  a1.Print();
  return 0;
 
 

error:
in main.cpp: undefined ref to A::Assign...
 
Thanks
Posted 21-Feb-13 11:20am
Edited 21-Feb-13 20:14pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

All correct. Well, included <stdlib.h> it totally redundant. You should not have this error. Maybe the files are actually different, or some files are not actually included in project, or something. The files themselves are correct. Well, try to recreate the project from scratch (with such files, don't use precompiled headers) accurately, with the same exact content; it will work.
 
—SA
  Permalink  
Comments
zhaoyilong1 at 22-Feb-13 2:14am
   
it can actually find the function, but says theres no definition of it~...
Sergey Alexandrovich Kryukov at 22-Feb-13 11:29am
   
You do have the definition. This is tiny code, and the problem can be only in project structure/options. It compiles if you do it right. Start over, make sure you understand each step; and use the code you already have. Ask a question is you don't understand some steps...
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

In Main.cpp, try using #include "A.h", not #include <A.h>.
Cheers
Andi
  Permalink  
Comments
zhaoyilong1 at 22-Feb-13 2:12am
   
oh. that's a typo. I did the right thing there, but it is not working ~ However when I include A.cpp rather than A.h, it all works...... it makes no sense to me now.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Have you included A.cpp in you project. Looks like the linker is complaining that it doesn't find the definition of A::Assign.
 
You should give us the exact error message that you are receiving. That could show if my assumption is true.
  Permalink  
Comments
enhzflep at 22-Feb-13 2:22am
   
exactly! That's where "undefined reference" comes from - the linker.
zhaoyilong1 at 22-Feb-13 2:43am
   
yea ~ I forgot to include a.cpp into makefile
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

How are you compiling your project?
If you're using an IDE, did you ensure that A.cpp is added to the project?
If you're compiling from the command line or a makefile, did you ensure you added both main.cpp and A.cpp as dependencies for the output executable?
  Permalink  
v2
Comments
zhaoyilong1 at 22-Feb-13 2:42am
   
yeayea ,,,, I forgot to compile a.cpp in my makefile ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 410
1 Gihan Liyanage 338
2 ChauhanAjay 180
3 Vinay Mistry 160
4 Sergey Alexandrovich Kryukov 138
0 Sergey Alexandrovich Kryukov 9,011
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100