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

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.

Comments
the dude knows what he's talkin about
This article has been dead for over six months. Start a new discussion instead.