Hello Everyone,
I am working on this Eclipse development tutorial from IBM.
I had already installed Eclipse prior to finding this tutorial and had installed MinGW instead of Cygwin. But other than that change, I followed the "Lottery" programming tutorial precisely until I got stuck at the Running the Program section.

Here I get several errors (most of which occur in the main.cpp)
-----------------------------------------------------------------------------------------
***main.cpp***
"class lotto::LotteryFactory has no member named 'getLottery'
"endl was not declared in this scope"
"scanf was not declared in this scope"
"expected primary-expression before '->'"
"expected primary-expression before ';'
"expected primary-expression before '='

***LotteryFactory.h****
"ISO C++ forbids declaration of 'Lottery' with no type"
"expected ';' before '*'"
-----------------------------------------------------------------------------------------

Here's my code:
___________________________________________________________________________
main.cpp

#include <iostream>
#include "LotteryFactory.h"

using namespace lotto;
int main()
{
	LotteryFactory* factory = LotteryFactory::getInstance();
	cout << "What lottery do you want to play?" << endl;
	cout << "(1) California, (2) Florida" << endl;
	int cmd;
	scanf("&d", &cmd);
	Lottery* lotto=0;
	switch (cmd)
	{
		case 1:
			lotto = factory->getLottery(LotteryFactory::California);
			break;
		case 2:
			lotto = factory->getLottery(LotteryFactory::Florida);
			break;
		default:
			cout<<"Sorry didn't understand that" << endl;
	}
	cout << "Ticket: " << lotto->printTicket() << endl;
	delete lotto;
	return 0;
}

______________________________________________________________________________

LotteryFactory.h:

#ifndef LOTTERYFACTORY_H_
#define LOTTERYFACTORY_H_

namespace lotto {

class LotteryFactory
	{
		public:
			enum State { California, Florida };
			static LotteryFactory* getInstance();
			Lottery* getLottery(State);
		private:
			LotteryFactory();
	};
}

#endif /* LOTTERYFACTORY_H_ */

__________________________________________________________________

Let me know if I should provide any further information about my errors.

Thank you!
L

Recommended Answers

All 4 Replies

You need to include "Lottery.h" in LotteryFactory.h (like it has it in the code listing on the site) since you have a Lottery object in the LotteryFactory class. You'll also still need to have Lottery.cpp as a part of your project for the methods of the Lottery class.

Including Lottery.h will also get rid of the errors surrounding cout, endl, etc. because it has the the statement using namespace std; Are you trying to learn the C++ along with how to use Eclipse? This tutorial presumes a lot of prior knowledge.

Hi jonsca,

Thanks for your help... and yes, I'm a new student to programming. As part of my internship, I was given the task of teaching myself C (though I realize this example is in C++), learning about Eclipse and writing an Android app. So I'm just trying to figure all this stuff out as I go along. :)

So... following your advice, I corrected my omission (#include "Lottery.h") then saved my changes. It got rid of most of the errors but I still have one in my main method at:

scanf("%d", &cmd);

the error reads: "'scanf' was not declared in this scope". It wasn't declared in the sample data... shouldn't scanf just be part of the c library?

Thanks again!

also, i do have Lottery.cpp in my project:

#include "Lottery.h"

namespace lotto
{
	Lottery::Lottery(int size, int num)
	{
		this->ticketSize=size;
		this->maxNum=num;
	}

	Lottery::~Lottery()
	{
	}

	int* Lottery::generateNumbers()
	{
		int* allNums = new int[this->maxNum +1];
		for (int i=0; i <= this->maxNum; i++)
			{
				allNums[i]=0;
			}
		int* nums = new int[this->ticketSize];
		for (int i=0; i<this->ticketSize; i++)
		{
			int val = 1 + rand() % (this->maxNum);
			if (allNums[val])
			{
				i--;
			}
			else
			{
				nums[i] = val;
				allNums[val]=1;
			}
		}
		return nums;
	}

	string Lottery::printTicket()
	{
		ostringstream str;
		int* nums = this->generateNumbers();
		for (int i = 0; i<this->ticketSize; i++)
		{
			str << *nums << ' ';
			nums++;
		}
		return str.str();
	}
}

I'm answering because you mentioned that you have to learn C. You should be cautious about how you learn to do things because C++ is a "superset" of C. This means that it incorporates C into it and expands upon it. I might suggest "C++: The Complete Reference" by Herbert Schildt. The first 1/3 or so of the book is dedicated to "The C Subset". It then goes on to introduce elements of C++ and compare and contrast them with the subset's capabilities.

Concerning your error:
The standard header <iostream> is a C++ header, it does not exist in C. The function scanf() is a C input function and not part of the <iostream> library. You will need to include the C standard input/output library to get it. In C, it's called "stdio.h" in C++ it's called <cstdio>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.