GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Jun-2006, 10:37
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Link Errors


Hi,

I am getting link errors when building an application. My code and the link errors are below.

//stocktank.h
CPP / C++ / C Code:
#include <string>

using namespace std;


class StockTank
{
private:
	int l,w,h,d,r,diam,percap;
	string shape;
	float vol;
	float totcapgal,totcapper;


public:
	StockTankCalculator();
	void SetTankShape(int s);
	void SetTankDataCircle();
	void SetTankDataCircle(int diam, int d);
	void SetTankDataRectangle();
	//void SetTankDataOval();
	float GetTankCapacity();
	float GetTankPercent();
	string GetTankData();

};



//driver.cpp
CPP / C++ / C Code:
#include "stocktank.h"
#include <string>
#include <iostream>

using namespace std;


int main()
{
	int ans;

	cout << "Jerry Brinegar - Program 3"<<endl;
	cout << "CP 278B C++ Programming II"<<endl;
	cout << "6/16/2006"<<endl;

	StockTank Calculator;
	//Calculator.SetTankDataCircle(int diam=72, int d=24);

	do
	{

	string info;


	cout << "Stock Tank Calculator"<<endl;
	cout << "Please select from one of the below options"<<endl<<endl;

	cout << "1. Set Tank Shape "<<endl;
	cout << "2. See Tank Information "<<endl;
	cout << "3. Get Tank Capacity Percentage "<<endl;
	cout << "4. Exit Program "<<endl<<endl;

	cout << "Enter your selection 1-4 here: ";
	cin >> ans;
	cout << '\n';

		switch(ans)

		{
		case 1:

			
			cout << "Stock Tank Shape Screen " << endl;
			cout << "Enter a tank type: " << endl;
			cout << "1. Circle" << endl;
			cout << "2. Rectangle" << endl;
			cout << "3. Oval" << endl;
			cout << "4. Return "<< endl;

			int ansshape;
		
			cin >> ansshape;

					
			switch (ansshape)
			{
				case 1:

				Calculator.SetTankShape(ansshape);
				//Calculator.SetTankDataCircle();
				//Calculator.GetTankCapacity();

				case 2:
				Calculator.SetTankShape(ansshape);
				//Calculator.SetTankDataRectangle();
				//Calculator.GetTankCapacity();
					
			}

		

			case 2:
		
				cout.setf(ios::fixed);
				cout.precision(2);

				info=Calculator.GetTankData();

				cout << info;

				break;

			case 3:
		
				Calculator.GetTankPercent();

 
				break;

			case 4:

				break;


			default:

		             cout << "You must enter a valid response!" << endl<<endl<<endl;
	
		
		}

	}while(ans!=4);

	return 0;
}

//stocktank.cpp
CPP / C++ / C Code:
#include "stocktank.h"
#include <string>
#include <iostream>
#include <cmath>

using namespace std;

double pi=3.14;

StockTank::StockTankCalculator()
{
	//hape="circular";
	//diam=72;
	//d=24;
	GetTankCapacity();
	GetTankData();



}

void StockTank::SetTankShape(int s)
{
		
	if (s=1)
	{
		shape="circular";
		//SetTankDataCircle();
	}
	else if (s=2)
	{
		shape="rectangular";
		//SetTankDataRectangle();
	}
	else if (s=3)
	{
		shape="oval";
	}


}


void StockTank::SetTankDataCircle()
{
	int diameter;
	int depth;
	shape="circlular";
	diameter=diam;
	depth=d;
	r=diam/2;


	StockTank::GetTankCapacity();


}

void StockTank::SetTankDataCircle(int diam, int d)
{
	int diameter;
	int depth;
	shape="circlular";
	diameter=diam;
	depth=d;
	r=diam/2;


	//StockTank::GetTankCapacity();


}
void StockTank::SetTankDataRectangle()
{
	int length=0, width=0, height=0;

	cout << "You selected a " << shape << " stock tank." << endl;

	cout << "Please enter length of rectangle: " << endl;
	cin >> length;
	cout << "Please enter width of rectangle: " << endl;
	cin >> width;
	cout << "Please enter height of rectangle: " << endl;
	cin >> height;

	l=length;
	w=width;
	h=height;

	//StockTank::GetTankCapacity();

}





float StockTank::GetTankCapacity()
{
	int tanktype=0;

	switch (tanktype)
	{

		case 1:
			vol=pi*(pow(r,2))*d;
			totcapgal=vol/231;
			break;

		case 2:
			vol=l*w*h;
			totcapgal=vol/231;
			break;

		default:
		break;
	}
	return totcapgal;
}

float StockTank::GetTankPercent()
{
	cout << "Enter a percentage in whole numbers (ex: 25 = 25%) ";
	cin >> percap;

	totcapper=(totcapgal*percap)*.01;

	cout << "The capacity of the " << shape << " tank at " << percap << "% is " << totcapper << endl << endl;

	return totcapper;

}

string GetTankData()
{
	string data;

	//data=shape + diam + d+ totcapgal;

	//cout.setf(ios::fixed);
	//cout.precision(2);
	//cout << "The Stock Tank type is " << shape << endl;
	//cout << "The tank dimensions are: " << diam <<"\" by " << d << "\"."<<endl; 
	//cout << "The Tank capacity (in gallons) of a "<< shape << " tank at 100 % "<<endl;
	//cout << "with these dimensions is: "<<totcapgal<<endl<<endl<<endl;

	return data;

}


The link errors are:

--------------------Configuration: BrinegarP3 - Win32 Debug--------------------
Linking...
driver.obj : error LNK2001: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall StockTank::GetTankData(void)" (?GetTankData@StockTank@@QAE?AV?$basic_string@DU?$ char_t
raits@D@std@@V?$allocator@D@2@@std@@XZ)
stocktank.obj : error LNK2001: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall StockTank::GetTankData(void)" (?GetTankData@StockTank@@QAE?AV?$basic_string@DU?$ cha
r_traits@D@std@@V?$allocator@D@2@@std@@XZ)
Debug/BrinegarP3.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

BrinegarP3.exe - 3 error(s), 0 warning(s)

What does this mean?
  #2  
Old 18-Jun-2006, 10:56
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 172
Blake will become famous soon enough

Re: Link Errors


The problem is in the GetTankData() function. I would guess that the problem is you are not initializing the string data.

You might consider using a stringstream there. That way you can use the << operator to concatonate data on it, just like cout.

You should consider using an enumerated typedef for the tank shape, like this:

In the header file:

CPP / C++ / C Code:
typedef enum {OVAL, RECTANGLE, CIRCLE} tankShape;
  #3  
Old 18-Jun-2006, 11:20
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Re: Link Errors


Okay, I see what you mean. But the reason I had everything in the GetTankData() function remarked is that I kept getting compile errors stating that the variables were undefined. I don't understand why, I thought that any public function of the class should be able to see the private data of the class. But when I assign the data variable I get:

CPP / C++ / C Code:
string GetTankData()
{
	string data="hi";

	data=shape;
	//cout.setf(ios::fixed);
	//cout.precision(2);
	//cout << "The Stock Tank type is " << shape << endl;
	//cout << "The tank dimensions are: " << diam <<"\" by " << d << "\"."<<endl; 
	//cout << "The Tank capacity (in gallons) of a "<< shape << " tank at 100 % "<<endl;
	//cout << "with these dimensions is: "<<totcapgal<<endl<<endl<<endl;

	return data;

}


--------------------Configuration: BrinegarP3 - Win32 Debug--------------------
Compiling...
stocktank.cpp
C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP3\stocktank.cpp(125) : error C2065: 'shape' : undeclared identifier
Error executing cl.exe.

stocktank.obj - 1 error(s), 0 warning(s


The specifications of the program states not to use enum and we haven't gone over it in class yet, although it looks to be easier.

Also, when I assign data a value, like above, I still get the link errors.
  #4  
Old 18-Jun-2006, 11:30
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 172
Blake will become famous soon enough

Re: Link Errors


You're missing the scope operator.

CPP / C++ / C Code:
string StockTank::GetTankData()

Without it the compiler doesn't know that GetTankData is a member function of the StockTank class.
  #5  
Old 18-Jun-2006, 11:55
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Re: Link Errors


Cool...thank you!
  #6  
Old 18-Jun-2006, 13:52
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Re: Link Errors


I'm pretty close to having everything working with this program but have a question regarding stringstream.

I'm utilizing this in my program, but one calculated variable I have (totcapgal) in the string StockTank::GetTankData()
is displaying on the screen as -1.07374e+008.

I'm using cout.setf(ios::fixed) and cout.precision(2) everywhere I can think of but it doesn't make a difference. Is there a way to use stringstream and format this variable as a fixed number with precision 2? Code is below:

//stocktank.cpp
CPP / C++ / C Code:
#include "stocktank.h"
#include <string>
#include <iostream>
#include <cmath>
#include <sstream>


using namespace std;

double pi=3.14;

StockTank::StockTankCalculator()
{
	shape="circular";
	diam=72;
	d=24;
	r=diam/2;
	GetTankCapacity();
	GetTankData();



}

void StockTank::SetTankShape(int s)
{
		
	if (s==1)
	{
		shape="circular";
	}
	else if (s==2)
	{
		shape="rectangular";
	}
	else if (s==3)
	{
		shape="oval";
	}


}


void StockTank::SetTankDataCircle()
{
	cout << "You selected a " << shape << " stock tank." << endl;

	int diameter;
	int depth;
	//diam=0;
	//diameter=diam;
	//depth=d;
	//d=0;

	cout << "Enter the diameter of the circle. " << endl;
	cin >> diameter;
	cout << "Enter the depth of the tank." << endl;
	cin >> depth;

	diam=diameter;
	d=depth;


}

void StockTank::SetTankDataRectangle()
{
	int length=0, width=0, height=0;



	cout << "You selected a " << shape << " stock tank." << endl;

	cout << "Please enter length of rectangle: " << endl;
	cin >> length;
	cout << "Please enter width of rectangle: " << endl;
	cin >> width;
	cout << "Please enter height of rectangle: " << endl;
	cin >> height;

	l=length;
	w=width;
	h=height;


}


void StockTank::SetTankDataOval()
{
	int length=0, width=0, height=0, radius=0;

	cout << "You selected a " << shape << " stock tank." <<endl;

	cout << "Please enter length of oval: " << endl;
	cin >> length;
	cout << "Please enter width of oval: " << endl;
	cin >> width;
	cout << "Please enter height of oval: " << endl;
	cin >> height;
	cout << "Please enter radius of oval: " << endl;
	cin >> radius;

	l=length;
	w=width;
	h=height;
	r=radius;
}



float StockTank::GetTankCapacity()
{
	int tanktype=0;

	switch (tanktype)
	{

		case 1:
			vol=pi*(pow(r,2))*d;
			totcapgal=vol/231;
			break;

		case 2:
			vol=l*w*h;
			totcapgal=vol/231;
			break;

		default:
		break;
	}
	return totcapgal;
}

float StockTank::GetTankPercent()
{
	cout << "Enter a percentage in whole numbers (ex: 25 = 25%) ";
	cin >> percap;

	totcapper=(totcapgal*percap)*.01;

	cout.setf(ios::fixed);
	cout.precision(2);

	cout << "The capacity of the " << shape << " tank at " << percap << "% is " << totcapper << endl << endl;

	return totcapper;

}

string StockTank::GetTankData()
{
	stringstream ss;
	
	cout.setf(ios::fixed);
	cout.precision(2);

	if (shape=="circular")
	{
		ss << "The Stock Tank type is " << shape << endl
		<< "The tank dimensions are: " << diam <<"\" by " << d << "\"."<< endl 
		<< "The Tank capacity (in gallons) of a "<< shape << " tank at 100 % "<< endl
		<< "with these dimensions is: "<< totcapgal << endl << endl << endl;

	}

	else if (shape=="rectangular")
	{
		ss << "The Stock Tank type is " << shape << endl
		<< "The tank dimentions are: " << l <<"\" by " << w << "\" by " << h << "\"."<< endl
		<< "The tank capacity (in gallons ) of a " << shape << " tank at 100 % " << endl
		<< "with these dimenstions is: " << totcapgal << endl << endl << endl;

	}

	string data=ss.str();

	return data;

}



The other files are:

//driver.cpp
CPP / C++ / C Code:
#include "stocktank.h"
#include <string>
#include <iostream>

using namespace std;


int main()
{
	int ans;

	cout << "Jerry Brinegar - Program 3"<<endl;
	cout << "CP 278B C++ Programming II"<<endl;
	cout << "6/16/2006"<<endl;

	StockTank Calculator;
	Calculator.StockTankCalculator();

	do
	{

	string info;


	cout << "Stock Tank Calculator"<<endl;
	cout << "Please select from one of the below options"<<endl<<endl;

	cout << "1. Set Tank Shape "<<endl;
	cout << "2. See Tank Information "<<endl;
	cout << "3. Get Tank Capacity Percentage "<<endl;
	cout << "4. Exit Program "<<endl<<endl;

	cout << "Enter your selection 1-4 here: ";
	cin >> ans;
	cout << '\n';

		switch(ans)

		{
		case 1:
			int ansshape;

			
			cout << "Stock Tank Shape Screen " << endl;
			cout << "Enter a tank type: " << endl;
			cout << "1. Circle" << endl;
			cout << "2. Rectangle" << endl;
			cout << "3. Oval" << endl;
			cout << "4. Return "<< endl;

		
			cin >> ansshape;

					
			switch (ansshape)
			{
				case 1:
					Calculator.SetTankShape(ansshape);
					Calculator.SetTankDataCircle();
					Calculator.GetTankCapacity();
					break;

				case 2:
					Calculator.SetTankShape(ansshape);
					Calculator.SetTankDataRectangle();
					Calculator.GetTankCapacity();
					break;

				case 3:
					Calculator.SetTankShape(ansshape);
					Calculator.SetTankDataOval();
					Calculator.GetTankCapacity();
					break;
					
			}
				break;
		

			case 2:

				info=Calculator.GetTankData();

				cout << info << endl;

				break;

			case 3:
		
				Calculator.GetTankPercent();

 
				break;

			case 4:

				break;


			default:

			cout << "You must enter a valid response!" << endl<<endl<<endl;
	
		
		}

	}while(ans!=4);

	return 0;
}

and

//stocktank.h
CPP / C++ / C Code:
#include <string>

using namespace std;


class StockTank
{
private:
	int l,w,h,d,r,diam,percap;
	string shape;
	float vol;
	float totcapgal,totcapper;


public:
	StockTankCalculator();
	void SetTankShape(int s);
	void SetTankDataCircle();
	void SetTankDataCircle(int diam, int d);
	void SetTankDataRectangle();
	void SetTankDataOval();
	float GetTankCapacity();
	float GetTankPercent();
	string GetTankData();

};

Thanks again for any help.
  #7  
Old 18-Jun-2006, 13:54
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Link Errors


You set cout.etc(), but you aren't using cout. You need to set ss.setf(ios::fixed) and ss.precision(2).
  #8  
Old 18-Jun-2006, 13:57
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 172
Blake will become famous soon enough

Re: Link Errors


^^beat me to it Also, there's no point in calling the copy constructor here:

CPP / C++ / C Code:
	string data=ss.str();

	return data;

Do this instead:

CPP / C++ / C Code:
	return ss.str();
  #9  
Old 18-Jun-2006, 14:04
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Re: Link Errors


Okay, I tried that and still get the -1.blah number

I've tried placing it in the conditionals as well.

CPP / C++ / C Code:
string StockTank::GetTankData()
{
	stringstream ss;
	
	ss.setf(ios::fixed);
	ss.precision(2);


	if (shape=="circular")
	{

		ss << "The Stock Tank type is " << shape << endl
		<< "The tank dimensions are: " << diam <<"\" by " << d << "\"."<< endl 
		<< "The Tank capacity (in gallons) of a "<< shape << " tank at 100 % "<< endl
		<< "with these dimensions is: "<< totcapgal << endl << endl << endl;

	}

	else if (shape=="rectangular")
	{

		ss << "The Stock Tank type is " << shape << endl
		<< "The tank dimentions are: " << l <<"\" by " << w << "\" by " << h << "\"."<< endl
		<< "The tank capacity (in gallons ) of a " << shape << " tank at 100 % " << endl
		<< "with these dimenstions is: " << totcapgal << endl << endl << endl;

	}

	return ss.str();
	

}


  #10  
Old 18-Jun-2006, 14:12
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 172
Blake will become famous soon enough

Re: Link Errors


My guess is that the variable is not being initialized. Try defining a default constructor. It looks to me like you're trying to do that with the function StockTankCalculator(). Turn that function in to your default contructor, and make sure you initialize all the variables.
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Code Snipet For Doubly Linked List Danny C Programming Language 8 19-Feb-2008 11:37
High quality link building spogg Search Engine Optimization Forum 7 23-Aug-2006 11:41
Compile Errors due to Default Parameters jdbrine C++ Forum 1 17-Jun-2006 14:45
Useful Free Link Exchange Program JC3 Member Announcements, Advertisements & Offers 0 23-Sep-2005 14:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:29.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.