1

There have been similar questions to this but they are all in C, rather than C++, so I have asked a new question.

I have been following a C++ tutorial and after completing the dynamic memory, pointers and structure sections I tried to put them together in an example program.

Essentially, I am trying to have a dynamically allocated array of a structure (the program inputs "produce" :P and displays the result).

The compiler errors: 'base operand of '->' has non-pointer type 'produce' for the code fruit[i]->item;

Sorry if the code is a bit long winded (I didn't want to leave out sections in case they were the problem, even if this results in the question being 'too localised'):

#include <iostream> #include <string> #include <new> using namespace std; struct produce { int price; string item; }; int main(void) { int num; int i; //Get int for size of array cout << "Enter the number of fruit to input: "; cin >> num; cout << endl; //Create a dynamically allocated array (size num) from the produce structure produce *fruit = new (nothrow) produce[num]; if (fruit == 0) { cout << "Error assigning memory."; } else { //For 'num', input items for (i = 0; i < num; i++) { cout << "Enter produce name: "; //Compiler error: 'base operand of '->' has non-pointer type 'produce' cin >> fruit[i]->item; cout << endl; cout << "Enter produce price: "; cin >> fruit[i]->price; cout << endl; cout << endl; } //Display result for (i = 0; i < num; i++) { cout << "Item: " << fruit[i]->item << endl; cout << "Cost: " << fruit[i]->price << endl; cout << endl; } //Delete fruit to free memory delete[] fruit; } return 0; } 
5
  • cin >> fruit[i].item; fruit[i] returns the reference to an object ,not a pointer to an object. So you have to use the dot operator, not the arrow operator. Commented Jul 10, 2013 at 17:32
  • You're creating an array of produce objects, each element in the array is a produce object. Commented Jul 10, 2013 at 17:34
  • In other words, fruit[i] is an instance of produce, it's not a pointer to produce. Using -> on it is thus wrong as the compiler already indicated. Commented Jul 10, 2013 at 17:37
  • Oh... i thought that fruit was a pointer Commented Jul 10, 2013 at 17:37
  • 2
    @user2569582... You thought right. fruit is a pointer. But fruit[i] is an object. Commented Jul 10, 2013 at 17:38

2 Answers 2

1

I see in your code produce *fruit, so, fruit is a pointer to one or more produce objects. That means that fruit[i] evaluates to a single actual produce object. Since it's an object, to access its item member, you use the . symbol. You would only use -> if it were a pointer. So you need to change fruit[i]->item; to fruit[i].item.

Sign up to request clarification or add additional context in comments.

Comments

1

Consider this simple example to make it explicit how you are accessing an object, not a pointer to an object:

int *arr = new (std::nothrow) int[10]; for(int i=0; i< 10 ; ++i) { arr[i]=i; } delete [] arr; 

Each element in arr( for instance arr[0]) is a simple int, for the sake of the example it is being initialized the content of each element of the array with the index value, later the array of ints is deleted. For you case each element in the fruit array (fruit[0], fruit[1], etc...) is an object of type produce (not a pointer to an object). Therefore the access must be with the access operator . instead of ->.

3 Comments

@user2569582 that's referring to std namespace.
@notNullGothik actually, his original code is abusing namespace std; so his nothrow isn't qualified with std.
@greatwolf Correct, since I didn't declare the using statement, I used the fully qualified version. For the original code, you can omit std.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.