How to use own definition header and body by separate ? (VC6)

Write function definition header in .h file, and write function definition body in .cpp file.
How can I make linker know the definition body in the .cpp ?

I new a Win32 Application project, and I add .h and .cpp in "Header Files" and "Resource Files" folder, but it still couldn't find the definition body when Build...
what did the error say?
does your .cpp include the .h ?
--------------------Configuration: Compiler - Win32 Debug--------------------
Linking...
Compiler.obj : error LNK2001: unresolved external symbol "char * * * __cdecl Allocate_3D(int,int,int)" (?Allocate_3D@@YAPAPAPADHHH@Z)
Debug/Compiler.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

Compiler.exe - 2 error(s), 0 warning(s)


yes, my .cpp include the .h
# include"array.h"
Last edited on
*Disch's heart stops a little bit at the sight of this "Allocate_3D" function*

Obligatory link: http://www.cplusplus.com/forum/articles/17108/


As for your actual problem:

"unresolved external symbol" errors typically mean you're trying to call a function which has a prototype, but no function body.

In this case, since you seem to have a function body in a .cpp file, make sure the function has the exact same name and exact same parameter list.

And in fact, if you want, you can post both the prototype (in the header) and the function definition (in the .cpp) so we can point out where the error is.
@OP, it looks like you are using VC++ so add the source for your functions into your project. That will link the source and implementation to your code.
And be sure to read Disch's article, it raises some important points about multi-dimensional arrays in C++.
My main function is in "Compiler.cpp"
"Allocate_3D" function prototype is in "array.h", body is in "array.cpp".

Compiler.cpp
char ***table = Allocate_3D<char>( t, r, c ) ;

array.h
1
2
template <class KeyType>
KeyType ***Allocate_3D( int z, int y, int x ) ;


array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# include<stddef.h>
# include<iostream.h>
template <class KeyType>
KeyType ***Allocate_3D( int z, int y, int x )
{
  int i, j ;
  KeyType ***f ;
  
  f = (KeyType***) malloc( z * sizeof(KeyType**) ) ;
  if(!f) return NULL ;
  
  for ( i = 0 ; i < z ; i++ ) {
    f[i] = (KeyType**) malloc( y * sizeof(KeyType*) ) ;
    if(!f[i]) return NULL ;
    
    for ( j = 0 ; j < y ; j++ ) {
      f[i][j] = (KeyType*) malloc( x * sizeof(KeyType) ) ;
      if(!f[i][j]) return NULL ;
    } // for
  } // for
  
  return f ;
} // KeyType ***Allocate_3D( int z, int y, int x ) 


//-----------

Another question:
include<> is write in .h or .cpp ?
I write a function prototype void DeWhiteSpace( ifstream & inFile ) ; in "input.h",
and write a function body :
input.cpp
1
2
3
4
5
6
7
8
9
void DeWhiteSpace( ifstream & inFile ) {
  char ch = inFile.peek() ;
  while ( ch != EOF &&
          ( ch == ' ' || ch == '\n' || ch == '\t' ) ) {
    inFile.get( ch ) ;
    ch = inFile.peek() ;
  } // while
  return ;
} // DeWhiteSpace() 

If I only write # include<fstream.h> once (in .h or .cpp), they also have compiler error.
But write the same include twice (in .h and .cpp), is standard writing ?
Last edited on
Compiler.cpp can't instantiate Allocate_3D<char> because it can't see the function body, because it's in a seperate file.

For templates, you typically have to put the function/class in the header, not in its own cpp file.

Also.. use code tags: http://cplusplus.com/articles/firedraco1/

Also: ew @ this concept. Srsly: http://www.cplusplus.com/forum/articles/17108/
oot: your heart is beating again?^^...
closed account (1yR4jE8b)
You cannot have a templated function in a seperate file, the function body must also be in the header file (save for a few dirty workarounds).
Oh... I see...
(templated function cannot separate in difference files...)


Then... Another question is about "include" is written in .h or .cpp ? or both??
input.h
void DeWhiteSpace( ifstream & inFile ) ;

input.cpp
1
2
3
4
5
6
7
8
9
void DeWhiteSpace( ifstream & inFile ) {
  char ch = inFile.peek() ;
  while ( ch != EOF &&
          ( ch == ' ' || ch == '\n' || ch == '\t' ) ) {
    inFile.get( ch ) ;
    ch = inFile.peek() ;
  } // while
  return ;
} // DeWhiteSpace() 

Must write # include<fstream.h> ...but where ?
Last edited on
there's no .h in fstream... it's just #include <fstream>

You can put it in the .h file for simplicity.
You can either put it in .h. Or put it in the .cpp where its actually used. However, then you would need to forward declare the ifstream in the .h.

Ideally, you only want to include headers when they are being used. i.e.

input.h
1
2
3
class std::ifstream;

void DeWhiteSpace(ifstream &);


input.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <fstream>

void DeWhiteSpace( std::ifstream & inFile ) 
{
  char ch = inFile.peek() ;

  while ( ch != EOF &&  ( ch == ' ' || ch == '\n' || ch == '\t' ) ) 
  {
    inFile.get( ch ) ;
    ch = inFile.peek() ;
  } // while
  return ;
}
Last edited on
Topic archived. No new replies allowed.