4 Replies - 290 Views - Last Post: 30 July 2014 - 08:50 PM Rate Topic: -----

#1 Geckoscraps  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 27-February 14

Linking Classes through Header Files

Posted 30 July 2014 - 07:51 PM

I am having the issue of trying to call functions of a class through the header file.

Test.h
#ifndef TEST_H
#define TEST_H

class Test
{
	public:
		void Hello();
};

#endif



Test.cpp
//Class Test
#include "Test.h"

void Test::Hello()
{
	std::cout << "Hello, From a Class Header/CPP File";
}



Tester.cpp
//Class Test
#include <iostream>
#include "Test.h"
using namespace std;

int main()
{
	Test tst;
	
	tst.Hello();
	
	return 0;
}



This code returns the error "undefined reference to 'Test::Hello()'"
All I am trying to do is call the
Hello();
function in a seperate file.
Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Linking Classes through Header Files

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,307
  • Joined: 21-June 11

Re: Linking Classes through Header Files

Posted 30 July 2014 - 08:03 PM

Your header file is fine. Linking is a completely separate issue. Which files are linked together has nothing to do with your code, but how you compile your program.

If you're compiling on the command line, you'll need to do something like g++ --your-usual-options Test.cpp Tester.cpp or g++ -c --whatever Test.cpp; g++ -c --bla Tester.cpp; g++ --options Test.o Tester.o. If you're using a Makefile, you'll need to adjust it accordingly. And if you're using an IDE, you'll need to make sure that all your cpp files are added to the same project.
Was This Post Helpful? 1
  • +
  • -

#3 Geckoscraps  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 27-February 14

Re: Linking Classes through Header Files

Posted 30 July 2014 - 08:17 PM

Thanks, I use an IDE so the project made it work, however now it gives the error in Test.cpp "cout was not declared in this scope" I tried adding std::, but that returned "cout is not a member of std" I am wondering if it would bring compiling errors having #include <iostream> in both tester.cpp and test.cpp?
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,307
  • Joined: 21-June 11

Re: Linking Classes through Header Files

Posted 30 July 2014 - 08:47 PM

You do need the include in both cpp files and it won't cause any errors. It is perfectly valid and common to include the same header file in multiple cpp files.
Was This Post Helpful? 0
  • +
  • -

#5 Geckoscraps  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 27-February 14

Re: Linking Classes through Header Files

Posted 30 July 2014 - 08:50 PM

Thanks :bigsmile: I think I'm starting to really grasp C++. Those suggestions worked and I think I understand classes now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1