varriable "" is not a type name

I'm attempting to pass a couple of variables over to my Item.cpp class, that is description and item_price. However under item.set(description,item_price), i get three errors. One under the . (period) saying : expected an identifier and two more under description and item_price stating that variable " xxx " is not a type name.
Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include <iostream>
#include "item.h"
using namespace std;
using namespace items;
int main(){
	 int n;
	// allocate static memory here
	cout << "Item Information\n";
	cout << "=====================" << endl;
	cout << "Number of Items : ";
	cin >> n;
	int * numItems;

	numItems = new int[n];
	for (int i = 0; i < n; i++){
		char description[31];
		double item_price;

		cout << "Enter name of the item : ";
		
		cin >> description;
	
		cout << "Enter Price: ";
		cin >> item_price;

		item.set(description, item_price);

	}
		
			
		return 0;
}


Item.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>
#include "item.h"
#include <cstring>
using namespace std;
using namespace items;

void item::set(const char* n, double item_price)
{
	double * itemprice;

	itemprice = new double[numItems];
	for (int i = 0; i < numItems; i++){
		
	}
	

}


item.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21


namespace items{
	class item{

	private:



		double itemPrice;

		char itemName[30] = "\0";
	public:
		int numItems;
		void set(const char description[], double item_price);
		void display() const;

	};
}



You have not created a variable named item anywhere.
item is a class
You have not created a variable named item anywhere.
item is referring to my item namespace/class
Non-static class member functions need an object (an instance of a class) to be called through.

The syntax for line 27 should be variable_name.member_function()
You need to make instance of your class,
for example
1
2
items::item my_item;
my_item.set(description, item_price);
Last edited on
Ohhh! thank you so much, how could i have missed that? thanks!
As they have mentioned and you ignored. In your main function you are using your item class as a variable. item.set(description, item_price); You must declare the class object first. Also you do realize creating that namespace is utterly useless if you are going to put using namespace items; it would be best to create the object like
1
2
3
items::item Item;

Item.set(description, item_price);
I'm getting another error at a different point

1
2
3
4
for (int i = 0; i < n; i++){
				myitem[i].display();
				cout << endl;
		}


the first bracket of [i] shows this error:
1 IntelliSense: no operator "[]" matches these operands
operand types are: items::item [ int ]
myitem is not an array, is not a vector, is not a map,
myitem is a `item', and items do not understand the operator[]
how would i go by sending the array number to display()?, i need to cycle through my description and itemprice array and display it accordingly in display()

Kinda stumped on how to get that to work.
Last edited on
You could try using the right name for your array...
Topic archived. No new replies allowed.