Using classes

For my assignment, it's a requirement to use classes. And I have functions that are meant to be in the classes defined as public functions. But when I call the functions in my main program or in other functions outside of the classes, Visual Studio says they're unidentified. Any way to fix this?

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
//this function is defined in a class
void setItem(int id, int h, int w, int l, int count, double p, double wt, string n, string r)
		{
			itemId = id;
			itemHeight = h;
			itemWidth = w;
			itemLength = l;
			inventoryCount = count;
			itemPrice = p;
			itemWeight = wt;
			itemName = n;
			itemRanking = r;
		}


And I call it in this function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void loadInventory(string fileName, int id, string n, int h, int w, int l, int count, double p, double wt, string r) 
	{
		ofstream outStream;
		ifstream inStream;

		inStream.open(" ");
		if (inStream.fail()) {
			cout << "Please enter the file name: " << endl;
			cin >> fileName;
		}
		
		setItem(id, h, w, l, count, p, wt, n, r); 
                //setItem is undefined here
	}
You say that setItem() is defined in a class, so I'm assuming your class looks something like this:

1
2
3
4
5
6
7
8
class MyClass {
public :
	setItem(/*blah*/) {
		itemId = id;
		itemHeight = w;
		//and so on
	}
};

Is this correct?
The reason I ask, is because beginners sometimes confuse the terms "declare" and "define". They also don't always create separate files to split class declarations from their implementations.

If my assumption is correct, then your error has to do with line 12 of the loadInventory() function (obviously). You're calling setItem() as if it were a global function, but it isn't. It's a member function, but you didn't declare any object of your class.

1
2
MyClass myclass;
myclass.setItem(/*arguments*/);//this is how you call a member function 
Last edited on
Yeah, that was the error. Thank you so much!
Topic archived. No new replies allowed.