Problems with Multi-File program

Problem 1 undefined functions?:

1
2
3
4
5
6
7
8
//swap.h
#ifndef SWAP_H
#define SWAP_H

template <class T>
void swap(T* a, T* b);

#endif 


1
2
3
4
5
6
7
8
9
10
//swap.cpp
#include "swap.h"

template <class T>
void swap(T* a, T* b)
{
  T temp = *a;
  *a = *b;
  *b = temp;
}


1
2
3
4
5
6
7
8
9
10
11
12
//main.cpp
#include "swap.h"

int main()
{
  int a, b;
  a = 5;
  b = 10;

  swap(&a, &b); // error: 'swap' was not declared in this scope
  return 0;
}


$ g++ -o myProgram main.cpp swap.cpp

Any ideas why I'm getting this error? Swap seems pretty declared to me...


Problem 2 default arguments:

1
2
3
4
5
6
7
//output.h
#ifndef OUTPUT_H
#define OUTPUT_H

void output(const char* input = "Hello World!"); // error: after previous specification in 'void output(const char*)'

#endif 


1
2
3
4
5
6
7
8
//output.cpp
#include "output.h"
#include <iostream>

void output(const char* input = "Hello World!") // error: default argument given for parameter 1 of 'void output(const char*)'
{
  std::cout << input << std::endl;
}


1
2
3
4
5
6
7
8
9
10
//main.cpp
#include "output.h"

int main()
{
  output();
  output("Goodbye World!");

  return 0;
}


$ g++ -o myProgram main.cpp output.cpp

Can I get some help deciphering those errors? I don't see a reason why I can't specify a default argument... It works fine if they aren't in separate files...

Any ideas? Thanks.
Last edited on
Problem 1:
You can't split template stuff in multiple files, move the implementation in the header
Problem 2:
You already said which is the default value for the argument in the declaration, remove = "Hello World!" from the cpp file
Last edited on
1. define the swap function in swap.h and do g++ main.cpp. this will solve the problem.

2. when you define the function you dont give default arguments there..only give default arguments during declaration.
1. Moved the definition to the .h file. Works, but begs the question why even bother with .cpp files in the first place if anything with templates can't be split up...

Thanks though.

2. Changed output.h to read
void output(const char* input); //Line 4
left output.cpp the same.

when I try compiling, it complains that at main.cpp:6 there are too few arguments to function 'void output(const char*)'
output(); //Line 6

I guess it didn't figure out that the function was overloaded?

Stumped on this one. Any other ideas or what..?
Last edited on
you did it otherway round..
your header file is correct:
1
2
3
4
5
6
7
//output.h
#ifndef OUTPUT_H
#define OUTPUT_H

void output(const char* input = "Hello World!"); 

#endif 


1
2
3
4
5
6
7
8
9
//change your .cpp file
//output.cpp
#include "output.h"
#include <iostream>

void output(const char* input) 
{
  std::cout << input << std::endl;
}

oh snap my bad on that one.

Thanks good call that fixed everything.
@Bazzy
It isn't true that templates cannot be split into seperate header and cpp files.

If you want to limit the types that can be used with the template you can put the
declarations in the h file, and the definition and explicit instantiations in the cpp file.
Foer example - a writer of a library may want to do this (supply the h file, and the cpp file(s) already compiled .lib).

Topic archived. No new replies allowed.