Unresolved Externals problem

Program works fine if I don't use a header file

Page 1 of 1

4 Replies - 1320 Views - Last Post: 21 May 2007 - 05:42 PM Rate Topic: -----

#1 falconheart   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 14-April 07

Unresolved Externals problem

Posted 19 May 2007 - 03:23 PM

I'm a CS student studying C++. I'm fairly new at this. I have a program that is complete and runs correctly if it is all one file. However when I split it into the three files (one header and two cpp files) as required by my assignment, it gives me two LNK2019 errors. I've tried to look up what the errors mean and I still don't get what I've done wrong. I'll post the code for the header file, and the two cpp files that go with it. Note that my assignment is the class, the main file is just there for testing purposes. I would greatly appreciate some help pointing out where I've gone wrong.

I'll post the error codes as well.

Test2 error LNK2019: unresolved external symbol "public: int __thiscall Poly::Coefficient(int)" ([email protected]@@[email protected]) referenced in function _main

Test2 error LNK2019: unresolved external symbol "public: void __thiscall Poly::changeCoefficient(int,int)" ([email protected]@@[email protected]) referenced in function _main

Test2 fatal error LNK1120: 2 unresolved externals




"Poly.h"
class Poly
	{
	
	public:


		int degree();


		int Coefficient(int);


		void changeCoefficient(int, int);


	};



"Poly.cpp"
#include <iostream>
#include "Poly.h"
using namespace std;



		struct polynomial //contains power and coefficient of term
		{
			int power;
			int coefficient;
		}
		polynomial x1, x2 ,x3;
		Poly::Poly()
		{
		x1.power = 1;
		x1.coefficient = 1;
		x2.power = 2;
		x2.coefficient = 2;
		x3.power = 3;
		x3.coefficient = 3;
		}
		int Poly::degree() // returns the degree of the polynomial
		{
			return (x1.power + x2.power + x3.power);
		}

		int Poly::Coefficient(int index) //returns the coefficient of a term
			{
	
				if(x1.power == index)
					return x1.coefficient;
				else if(x2.power == index)
					return x2.coefficient;	
				else if(x3.power == index)
					return x3.coefficient;
				else
					cout << "This variable does not exist. Returning zero by default.";
					return 0;

			}

			void Poly::changeCoefficient(int index, int newCoefficient) //changes the coefficient of a term
		{
			
			if(x1.power == index)
				x1.coefficient = newCoefficient;
			else if(x2.power == index)
				x2.coefficient = newCoefficient;	
			else if(x3.power == index)
				x3.coefficient = newCoefficient;
			else
				cout << "This variable does not exist. Unable to replace value.";
		}



"main.cpp"
#include <iostream>
#include "Poly.h"
using namespace std;



int main()
{
	
	Poly pol;
	int index, newCoefficient;
	char answer;
	do{
		cout << "Enter a power of a variable to see it's coefficient: ";
		cin >> index;
		cout << pol.Coefficient(index) << endl;
		cout << "Enter a power of a variable to change it's coefficient: ";
		cin >> index;
		cout << endl << "Enter the new coefficient desired: ";
		cin >> newCoefficient;
		pol.changeCoefficient(index, newCoefficient);
		cout << "Do you want to loop this program?" << endl;
		cin >> answer;
	}while(answer == 'y');

	system("PAUSE");
	return 0;
}



This post has been edited by falconheart: 19 May 2007 - 03:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Unresolved Externals problem

#2 Pontus   User is offline

  • Cattlebruiser

Reputation: 18
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: Unresolved Externals problem

Posted 20 May 2007 - 12:47 AM

Your poly.h needs to look like this:
#ifndef POLY_H
#define POLY_H
class Poly
	{
	public:
		int degree();
		int Coefficient(int);
		void changeCoefficient(int, int);
	};
#endif


This post has been edited by manhaeve5: 20 May 2007 - 12:47 AM

Was This Post Helpful? 0
  • +
  • -

#3 falconheart   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 14-April 07

Re: Unresolved Externals problem

Posted 20 May 2007 - 07:08 PM

Hmm.. Thank you for your input; however it did not solve the unresolved externals problem when I applied it to my program. Do you see anything else that could be causing this problem?
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Unresolved Externals problem

Posted 20 May 2007 - 08:11 PM

My guess is that you have bigger fish to fry. Polly.cpp didn't compile and that is why it cannot find the functions.

First of all you need to change the declaration of polynomial and add it to the header file. Those polynomials that you declared global need to put inside of the class. YOur polly header should look more like this:
#ifndef POLY_H //Ensures that this header is never included twice in the same project...
#define POLY_H

//This needs to be declared here because the variable x1, x2, x3 must be declared inside the class
typedef struct  //contains power and coefficient of term
{
	int power;
	int coefficient;
} polynomial;

class Poly
{
private:
	//These variables can not be global
	polynomial x1, x2 ,x3;	
public:
	Poly();
	int degree();
	int Coefficient(int);
	void changeCoefficient(int, int);
};

#endif

the compiler directives #ifndef, #define, and #endif are not NEEDED, but they do help. In large projects when headers include header that include headers it can be hard to remeber if you already declared something... this ensures that only 1 copy of the header gets included.

you class file should then look like:
#include <iostream>
#include "Poly.h"
using namespace std;


Poly::Poly()
{
	x1.power = 1;
	x1.coefficient = 1;
	x2.power = 2;
	x2.coefficient = 2;
	x3.power = 3;
	x3.coefficient = 3;
}

int Poly::degree() // returns the degree of the polynomial
{
	return (x1.power + x2.power + x3.power);
}

		int Poly::Coefficient(int index) //returns the coefficient of a term
			{
	
				if(x1.power == index)
					return x1.coefficient;
				else if(x2.power == index)
					return x2.coefficient;	
				else if(x3.power == index)
					return x3.coefficient;
				else
					cout << "This variable does not exist. Returning zero by default.";
					return 0;

			}

void Poly::changeCoefficient(int index, int newCoefficient) //changes the coefficient of a term
{
	
	if(x1.power == index)
		x1.coefficient = newCoefficient;
	else if(x2.power == index)
		x2.coefficient = newCoefficient;	
	else if(x3.power == index)
		x3.coefficient = newCoefficient;
	else
		cout << "This variable does not exist. Unable to replace value.";
}


Make sure that it is included in the project. You should see the compiler compile it (it should have given you an error since you were missing a semicolon and didn't declare your polynomials correctly).
Was This Post Helpful? 0
  • +
  • -

#5 falconheart   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 14-April 07

Re: Unresolved Externals problem

Posted 21 May 2007 - 05:42 PM

Thank you!! It works! <dance> It works! <dance> :)
Take care & God bless!
--Falconheart
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1