Reputation: 67
My code was
#include <iostream>
#include <string>
using namespace std;
struct Numbers{
int a;
int b;
int c;
};
struct NumbersArray{
int size;
Numbers *numbers;
};
int main(){
NumbersArray numArr;
numArr.size = 10;
numArr.numbers = new Numbers[10];
for(int i = 0; i < 10; i++){
Numbers *num = new Numbers;
num->a = i * 3 + 0;
num->b = i * 3 + 1;
num->c = i * 3 + 2;
numArr.numbers[i] = num;
}
}
The basic idea is I create a struct called Numbers which contains 3 numbers and put it in a wrapper struct called NumbersArray. And when I compile it with g++, I got error message
testArrayStruct.cc: In function ‘int main()’:
testArrayStruct.cc:23:27: error: no match for ‘operator=’ (operand types are ‘Numbers’ and ‘Numbers*’)
numArr.numbers[i] = num;
^
testArrayStruct.cc:23:27: note: candidate is:
testArrayStruct.cc:4:8: note: Numbers& Numbers::operator=(const Numbers&)
struct Numbers{
^
testArrayStruct.cc:4:8: note: no known conversion for argument 1 from ‘Numbers*’ to ‘const Numbers&’
Just ignore memory management here. I cant figure it how to add elements to the array.
Upvotes: 0
Views: 205
Reputation: 11251
This is the important part of your error message:
error: no match for ‘operator=’ (operand types are ‘Numbers’ and ‘Numbers*’)
You are trying to assign a pointer to Numbers
into a Numbers
value. The rest of the error message is not helpful.
Assuming you don't want to change the NumbersArray
class like all the other answers are suggesting, your inner loop should look like this:
for(int i = 0; i < 10; i++){
Numbers num;
num.a = i * 3 + 0;
num.b = i * 3 + 1;
num.c = i * 3 + 2;
numArr.numbers[i] = num;
}
Read about values and pointers in C++.
Upvotes: 0
Reputation: 7644
You are practising on the wrong things, and you will just pick up bad habits this way. C++ thrive on value semantics, focus on that:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Numbers {
int a;
int b;
int c;
};
using NumbersArray = vector<Numbers>;
int main()
{
NumbersArray numArr;
numArr.resize(10);
for(int i = 0; i < 10; i++)
{
Numbers num;
num.a = i * 3 + 0;
num.b = i * 3 + 1;
num.c = i * 3 + 2;
numArr[i] = num;
}
}
Upvotes: 1
Reputation: 6875
It should be (ignoring your memory management as you asked):
numArr.numbers[i] = *num;
Instead of
numArr.numbers[i] = num;
since numArr.numbers[i]
is of type Numbers
while num
is of type Numbers*
.
EDIT:
The error actually tells you that there is no operator=
to perform assignment of Numbers *
to Numbers
. So actually you could implement your own operator=
which would perform such operation:
Numbers& operator=(Numbers& left, const Numbers * const right)
{
if (right != NULL) // or nullptr for C++11
{
left = *right;
}
return left;
}
With such implementation your original code would also compile.
One remark regarding the memory management. There is no real need to allocate new Numbers
inside the for loop since you copy that content into the numArr.numbers
anyway. So you could write it that way:
for(int i = 0; i < 10; i++){
numArr.numbers[i].a = i * 3 + 0;
numArr.numbers[i].b = i * 3 + 1;
numArr.numbers[i].c = i * 3 + 2;
}
Upvotes: 0
Reputation: 599
Try the following code. You are try to assign a pointer of an object to the object itself. The following one fixes it.
#include <iostream>
#include <string>
using namespace std;
struct Numbers{
int a;
int b;
int c;
};
struct NumbersArray{
int size;
Numbers **numbers;
};
int main(){
NumbersArray numArr;
numArr.size = 10;
numArr.numbers = new Numbers* [10];
for(int i = 0; i < 10; i++){
Numbers *num = new Numbers;
num->a = i * 3 + 0;
num->b = i * 3 + 1;
num->c = i * 3 + 2;
numArr.numbers[i] = num;
}
}
Upvotes: 0
Reputation: 42838
how to add elements to the array
You can't.
When you said new Numbers[10]
you created an array of ten default-initialized elements. An array can't resize itself after initialization.
It sounds like you should be using std::vector
:
int main() {
std::vector<Numbers> numArr;
for(int i = 0; i < 10; i++) {
Numbers num;
num.a = i * 3 + 0;
num.b = i * 3 + 1;
num.c = i * 3 + 2;
numArr.push_back(num);
}
std::cout << "numArr contains " << numArr.size() << " elements." << std::endl;
}
Now you can add as many elements as you like and don't have to keep track of size
etc. yourself. std::vector
will resize itself when it needs to.
Upvotes: 0