Reputation: 435
I am learning C++ through a series of questions from the university I attend. Here is an excerpt of the problem in question:
A retailer shop wishes to store information about its sales. Typical information it wishes to store about a sale are: Sale identification number (a unique number for each sale), customer’s surname and other names as well as the customer’s address. A sale can consist of up to 10 different items, with different quantities of each item. For each item, the sale class should store the item number, item description, unit cost and the number of units of the items purchased as well as the total cost for the item.
TLDR: I am trying to write a constructor for a class which contains an array as one of its attribute.
I am learning about Classes in C++ and am currently trying out interface.
For the sake of clarity, here is a simplified version of the interface showing only the constructor:
Sale.h
class Sale {
int* products_id[];
public:
Sale(int& products_id[]);
};
Now, the interface needs to be implemented:
Sale.cpp
#include "Sale.h"
Sale::Sale(int& product_id[]) {
this->product_id[] = product_id[];
};
For some reasons, my IDE (VS Code) is complaining about an error
no suitable conversion function from "product_id" to "product_id *" exists
I am unable to find any relevant resources online.
I would appreciate it if someone could help me out or at least point me in the right direction. Regards.
Upvotes: 0
Views: 2443
Reputation: 149025
I shall start here at your first sentence:
I am trying to write a constructor for a class which contains an array as one of its attribute.
This is brave... Arrays are not first class elements of the language, and are often left hidden in standard containers. A general rule in C++ is: if you have no strong reason to use a raw array in C++, just stick to containers. Exceptions are:
Problems with arrays that are solved with containers:
std::vector
herestd::array
is enough hereSo a class that contains an array as one of its attributes should not be an ordinary common class. It should either be a container with the copy/move/destructor problem (search for rule of 3, rule or 5). Or the size must be constant for coordinates for example.
Upvotes: 1
Reputation: 457
First part: Pointer and C-Array
I guess you learned Java before writing such code snippet.
In Java, we have the handlers (or pointers to some extent) to each object, but in C++ we transmit objects directly by copying (without reference notation or redefinition of operator=
). Therefore, the Java-style Type arr[]
is misused in your code, and it has totally different meanings according to C/C++ contexts:
For instance:
#include <iostream>
using std::cout, std::endl;
int calc_sum(int arr[], int len) { // pointer
int ans = 0;
for (int i = 0; i < len; i++) {
ans += arr[i];
}
return ans;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(int);
cout << n << endl;
cout << calc_sum(arr, n) << endl;
return 0;
}
Second part: Reference
When you write code like int a = 3; int &b = a;
, you intended to make b
an alias for a
which means "now a
and b
are same". Therefore int& products_id[]
means a pointer to the reference of int
. And I think that is not what you want.
Upvotes: 1