Want to access a method form .cpp file

Guys,
Just want to know how to acess a method from a .h file in another .cpp file
Correct my code below show.


Ex:

// main.cpp

#include <iostream>
#include "inc/one.h"
using namespace std;
int main( )
{

School Tadd;
Tadd.AddTeacher(1);
return 0;
}

*********************************
//one.h

#ifndef ONE_H_INCLUDED
#define ONE_H_INCLUDED


using namespace std;

class School
{

public:
School();
void AddTeacher(int );
};

//constructor
School::School() {};

******************************

//one.cpp
#include "inc/one.h"
School::AddTeacher (int choice)
{

cout << "Adding a teacher \n";

return 0;
}

***********************

when i compile , i am getting the below error

In function main:
undefined reference to `School::AddTeacher(int)'
Build finished: 1 error
Last edited on
The definition of School::AddTeacher should look like this:
1
2
3
4
5
6
void School::AddTeacher( int choice ) // here you forgot the return type
{
    cout << "Adding a teacher\n";

    return 0; // you marked it as void, so don't return anything
}
Thank for the reply .. but that is not the fix for it ... while posting i forgot to add void as a return type
try copying the code ,compile and see .. let me know ur results
When I tried to compile, I got some more errors, but this is the working version:

one.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef ONE_H_INCLUDED
#define ONE_H_INCLUDED

class School
{
public:
	School();
	void AddTeacher(int );
};

#endif 


one.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "one.h"
#include <iostream>

using namespace std;

School::School() { }

void School::AddTeacher (int choice)
{
	cout << "Adding a teacher \n";
}


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

int main( )
{
	School Tadd;
	Tadd.AddTeacher(1);

	return 0;
}
Topic archived. No new replies allowed.