5 Replies - 924 Views - Last Post: 16 July 2010 - 11:18 PM Rate Topic: -----

#1 Cowjuicer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-December 08

Trouble creating an instance of a class

Posted 16 July 2010 - 09:12 PM

Hello there. I'm taking a class to learn C++ during the summer quarter, and I need some help figuring out why I can't create an instance of my class to test it.

Below are the contents of my header and source files:

Header:

// auction.h
//
// ICS 80 Summer 2010
// Project #1: Land of Hope and Dreams
//
// Name: 
// Student ID: [PUT YOUR STUDENT ID# HERE]
//
// [IF YOU HAVE A PARTNER FOR THIS PROJECT, INCLUDE HIS/HER NAME AND
//  STUDENT ID# HERE, AS WELL]
//
// Header file containing a declaration of the Auction class, which
// encapsulates bid-handling algorithms for an online auction.

#ifndef AUCTION_H
#define AUCTION_H

#include <string>



class Auction
{
public:
	// The constructor for an Auction takes the opening bid amount as
	// a parameter.
	Auction(unsigned int openingBidAmount, std::string& highBidderName);

	// It is doubtful that the destructor will need to do anything, but
	// we'll declare it for the sake of completeness.
	~Auction();

	// getHighBidderName() returns the name of the current high bidder,
	// or "" if no one has bid on this auction.
	std::string getHighBidderName() const;

	

	// getCurrentBidAmount() returns the current bid amount for this auction.
	// (Bear in mind that this is not the same as the proxy bid amount,
	// which may be over and above the current bid.)
	unsigned int getCurrentBidAmount() const;

	//setHighBidderName() is used within the bid method.
	std::string setHighBidderName(std::string changedBidder);

	//setCurrentBidAmount() is used within the bid method.
	unsigned int setCurrentBidAmount(unsigned int changedBid);

	// getMinimumBidAmount() returns the minimum bid that may be placed at
	// this time on the auction.  This will be either the opening bid amount
	// (if no bids have been placed) or the current bid plus the bid increment
	// (if bids have been placed).
	unsigned int findMinimumBidAmount();	
	
	// bid() takes a bidderName and a bidAmount.  If the bid is a legal one
	// (i.e., it's not too small), the necessary action is taken.
	void smallestBid(const std::string& bidderName);

	// getOpeningBidAmount() returns the opening bid for this auction.
	unsigned int getOpeningBidAmount() const;

	//formats the bid amount into regular money format
	std::string formatBidAmount(unsigned int bidAmount);

	//returns the minimum bid increment that must be added to a bid
	unsigned int getBidIncrement();

	void bidUI(const std::string& bidderName);

	void proxyBid(const std::string& bidderName, unsigned int proxyBid);

private:
	//member variables go here
	unsigned int openingBid;
	int numBids;
	unsigned int currentBidAmount;
	std::string highestBidderName;
	


private:
	// declare functions that are internal only
	
};



#endif // AUCTION_H



And here is my source file for that:
#include "auction.h"

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;

Auction::Auction(unsigned int openingBidAmount, string& highBidderName)
	: openingBid(openingBidAmount)
{
	currentBidAmount = openingBidAmount;
	highestBidderName = "";
	numBids = 0;

}

unsigned int Auction::getOpeningBidAmount() const
{
	return openingBid;
}

unsigned int Auction::getCurrentBidAmount() const
{
	return currentBidAmount;
}

unsigned int Auction::setCurrentBidAmount(unsigned int changedBid)
{
	currentBidAmount = changedBid;
}

string Auction::setHighBidderName(string changedBidder)
{
	highestBidderName = changedBidder;
}


unsigned int Auction::getBidIncrement()
{
	unsigned int currentBid = getCurrentBidAmount();
	unsigned int bidIncrement;
	if (currentBid < 99)
		bidIncrement = 5;
	else if (currentBid < 499)
		bidIncrement = 25;
	else if (currentBid < 2499)
		bidIncrement = 50;
	else if (currentBid < 9999)
		bidIncrement = 100;
	else if (currentBid < 24999)
		bidIncrement = 250;
	else if (currentBid < 49999)
		bidIncrement = 500;
	else if (currentBid < 99999)
		bidIncrement = 1000;
	else if (currentBid < 249999)
		bidIncrement = 2500;
	else if (currentBid < 499999)
		bidIncrement = 5000;
	else
		bidIncrement = 10000;

	
	return bidIncrement;
}

unsigned int Auction::findMinimumBidAmount()
{
	if(numBids=0)
	{
		return openingBid;
	}
	else
	{
		unsigned int basicBid = getCurrentBidAmount();
		unsigned int bidIncrement = getBidIncrement();
		unsigned int minimumBidAmount = basicBid+bidIncrement;
		return minimumBidAmount;
	}
}

string Auction::getHighBidderName() const
{
	return highestBidderName;
}


void Auction::smallestBid(const string& bidderName) 
{	
	unsigned int minBid;
	if(numBids=0)
		minBid = openingBid;
	else
		minBid = findMinimumBidAmount();
	highestBidderName = bidderName;
	currentBidAmount = minBid;
}	

void Auction::proxyBid(const string& bidderName, unsigned int proxyBid)
{
	unsigned int maxBid = proxyBid;

	while(currentBidAmount <= currentBidAmount)
	{
		if(currentBidAmount = maxBid && highestBidderName != bidderName)
		{
			highestBidderName = bidderName;
			numBids++;
		}
		else if(currentBidAmount<maxBid && highestBidderName != bidderName)
		{
			highestBidderName = bidderName;
			smallestBid(bidderName);
			numBids++;
		}
	}
}


string Auction::formatBidAmount(unsigned int bidAmount)
{
    ostringstream s;

    s << "$" << (bidAmount / 100) << ".";
    s << setw(2) << setfill('0') << (bidAmount % 100);

    return s.str();
}



void Auction::bidUI(const string& bidderName)
{
	int reader;
	while(reader != 3)
	{
		unsigned int minBid = findMinimumBidAmount();
		cout << "Welcome to the auction, " << bidderName << ". /endl"
		"The current high bid is " << formatBidAmount(currentBidAmount) << ", which was placed by " << highestBidderName << " . /endl"
		"If you would like to place the minimum bid of " << formatBidAmount(minBid) << " then simply press 1 and then RETURN. /endl"
		"Otherwise, you're placing a proxy bid, which will automatically bid for you up to a limit. Simply press 2 and then RETURN. /endl"
		"Finally, if you're ending this auction, press 3 and then RETURN to see results and lock in the high bid.";
	
		cin >> reader;
		if(reader = 1)
		{
			smallestBid(bidderName);
			cout << "Thank you for placing a bid! A proxy bid may have triggered, so be sure to check the highest bidder's name when it reappears. Hopefully it'll be you!";
			numBids++;
			continue;
		}
		else if(reader = 2)
		{
			unsigned int proxyAmount;
			cout << "Please enter the highest amount of money you wish to pay. This number MUST be in cents OR ELSE.";
			cin >> proxyAmount;
			proxyBid(bidderName, proxyAmount);
			cout << "Thank you for placing a bid! Hopefully you will win the item.";
			continue;
		}
		else
		{
			cout << "We have a winner! " << highestBidderName << " has won the auction with a bid of " << formatBidAmount(currentBidAmount) << " ! /endl"
				"Congratulations! Press ENTER to exit.";
			char s;
			cin.get(&s, 1);
			exit(0);
			
		}

	}
}



Finally, here is my main file. This is where the problem is: I am trying to construct an instance of my Auction class, but I receive an error of "unresolved external symbol" and "fatal error".

#include <iostream>
#include <string>
using namespace std;

#include "auction.h"

int main()
{
	
	string name = "John Smith"; //this string, and the unsigned int below it, are for ease of use while testing
	unsigned int money = 500;
	Auction potato(money, name);


	return 0;
}



If anyone can please help me understand/fix what is wrong, I would be very grateful. Judging by what I have read, I seem to be doing it right, but it just isn't compiling!

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble creating an instance of a class

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Trouble creating an instance of a class

Posted 16 July 2010 - 10:28 PM

Copy paste exact error here if you want help.
Was This Post Helpful? 1
  • +
  • -

#3 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2156
  • View blog
  • Posts: 4,219
  • Joined: 30-May 10

Re: Trouble creating an instance of a class

Posted 16 July 2010 - 10:31 PM

Let's say you have auction.cpp and auction.h as the first two files.

And the last one is called main.cpp

Now, since you didn't say anything about which OS/Compiler you're using, I'll assume visual studio. It doesn't matter, the task remains the same, it's only the UI that is different.

What you need to do is something like project->settings->source->Add... to make sure auction.cpp AND main.cpp are part of the project.

When you do a "rebuild", you should see
Compiling...
Auction.cpp
Main.cpp
Linking...


If you don't see ALL your .cpp files in the compiling stage, then you'll get unresolved symbols at link time.
Was This Post Helpful? 0
  • +
  • -

#4 Cowjuicer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-December 08

Re: Trouble creating an instance of a class

Posted 16 July 2010 - 10:41 PM

Apologies - I am indeed using Visual Studio 2010. My exact error is as follows:

1>------ Build started: Project: 80Project, Configuration: Release Win32 ------
1> auction.cpp
1> main.cpp
1>main.obj : error LNK2001: unresolved external symbol "public: __thiscall Auction::~Auction(void)" ([email protected]@[email protected])
1>C:\Users\Name\Documents\C++ Class (ICS 80)\80Solution2010\80Solution\Release\80Project.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The method provided by Salem_c (adding the .cpp files to the project) was not successful. I am still receiving the error.
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Trouble creating an instance of a class

Posted 16 July 2010 - 10:54 PM

You wrote in your code:

    // It is doubtful that the destructor will need to do anything, but
	// we'll declare it for the sake of completeness.
	~Auction();



Your intentions are well meaning, but you achieved the wrong syntactical effect. A default deconstructor is automatically provided when you don't provide one. You don't need to inform the compiler about it! As you can imagine, a C++ compiler will follow C++ rules, including that of providing a default constructor.

What you did with your declaration was effectively say that "hey, I actually defined a destructor somewhere". Which you didn't.

It's possible to reach this conclusion from reading the error. Consider: "main.obj : error LNK2001: unresolved external symbol "public: __thiscall Auction::~Auction(void)" ([email protected]@[email protected])"

What is this error? if you Google, or search on LNK2001, you reach the MSDN page which lists the various reasons you can get the error, including what's at hand here.

unresolved external symbol. Well, you specified the existence of a destructor, so there's a symbol that represents that destructor function. It's external, because you provided a declaration, saying, "oh, it exists somewhere", and it's unresolved, because well, the compiler can't find the definition. It then lists the function in question, which you should recognize as the destructor. Followed by the symbol name. The weird name, @@QAE portion is because of name mangling. You can certainly Google about name mangling. It's also discussed on that MSDN page.

So you see, if you have a problem, you can certainly diagnose it on your own.
Was This Post Helpful? 0
  • +
  • -

#6 Cowjuicer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-December 08

Re: Trouble creating an instance of a class

Posted 16 July 2010 - 11:18 PM

I did notice the portion of the error mentioning the destructor, and the portion with LNK2001. I guess I just didn't do enough reading/connecting the dots. Thank you very much - now I can turn my attention to fixing my rather poor code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1