The code posted below is giving an unusual error which i cant get to figure out. According to me the error is caused by the definition(or maybe by the initialisation of the string array). The error is comin in the vending.cpp file in the initialisation of the product[10] array

[B]vending.h  //header file[/B]
#ifndef  VENDING_HEADER
#define VENDING_HEADER  


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

class vendingM
{
	int price;
	string products[10];
	int quantity;


public:
	vendingM(void);
	void displayList();
	int quantityOfProducts();
	void getItem(string );

	
	~vendingM(void);
};

#endif VENDING_HEADERer file




[B]vending.cpp   //cpp file[/B]

#include "vending.h"

vendingM::vendingM()
{
	products[] = {"mars", "snickers" "bounty", "galaxy", "twix", "walkers", "mccoy", "pepsi", "coke", "sprite"};

}

void vendingM::displayList()
{
	for(int x=0; x<2; x++)
	{
		cout << products[x];
		cout << endl;
	};
}

vendingM::~vendingM()
{
}


[B]main.cpp   //main file[/B]

#include "vending.h"

int main()
{

	vendingM vmObject;
	vmObject.displayList();



	system("pause");
	return 0;
};





//////////// error

Error	3	error C2143: syntax error : missing ';' before '}'	d:\compiling test frm run\vending machine\vending machine\vending.cpp	5


Error	2	error C2143: syntax error : missing ';' before '{'	d:\compiling test frm run\vending machine\vending machine\vending.cpp	5


Error	1	error C2059: syntax error : ']'	d:\compiling test frm run\vending machine\vending machine\vending.cpp	5

Recommended Answers

All 2 Replies

The problem is occurring because you are using the initialization syntax for what is in fact not initialization. When you are setting the value of a variable that is declared elsewhere, you cannot use the initialization syntax for an assignment, even if you had not yet set the variable. You need to set each member of the array separately.

commented: the dude knows what he's talkin about +0

Cheers mate..it worked..thnx once again :)

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.