Reputation: 3
So I'm following a certain tutorial series and trying to use that knowledge to put my own spin on something kindof both similar but also different to the series itself. In one section, to tackle a certain inconvenience which is not important for the scope of my issue, a possible solution was to store certain chunk of data into members of a struct and then call that struct onto a function that later output the data precisely the way we wanted - as an array.
All was well and good, until the function for some reason refused to take in values of the array, claiming there was an overload of information in the array arguments (I will specify the errors later), and that the entity I am returning does not match the function's return type.
Here's my code:
struct TriCoord
{
float Position[3];
int Label[2];
};
std::array<TriCoord, 3> Implement(float x, float y, float z, int j) //2
{
float size = 1.0f;
TriCoord f0;
f0.Position = { x, y, z }; //1
f0.Label = { j, 1 }; //1
TriCoord f1;
f1.Position = { x + 0.5, y + 0.5, z };
f1.Label = { j, 2 };
TriCoord f2;
f2.Position = { x + 0.5, y, z };
f2.Label = { j, 3 };
return { f0, f1, f2 }; //2
};
int main()
{
int X;
float A[3] = Implement(3, 4, 5, 1); //3
std::cout << A[0] << ", " << A[1] << ", " << A[2] << std::endl;
std::cout << "Press Enter key to return" << std::endl;
std::cin >> X;
return 0;
}
I've put comments locating specifically where the errors are.
1) The compiler says that f0 is "not a modifiable lvalue". I see no reason why it shouldn't be. In the array argument at specifically the second element of the array (in this case y), the compiler says that I am adding "too many initializer values". Again, makes no sense. I specified the Position array to have 3 elements and am plugging 3 elements of matching type (float).
These two errors extend to f0.Label, as well as the other TriCoords.
2) The compiler says that the function "returns incomplete type 'std::array'". Furthermore, in the return line, "list-initialization of an object type 'std::array' is not allowed because the type is incomplete".
Technically what I am returning is an std array whose elements are TriCoords and of amount 3, so I don't see the problem once again. I did work around this issue by changing the function's return type to float, but in its stead the return line generates the same error for the second element (in this case f1) as in problem 1.
3) "Initialization with '{...}' expected for aggregate object" I have no idea what this means. I get this error regardless of which return type I specify for my function.
I must emphasise that both me and the series I'm following are using Visual Studios and no such errors popped up in the video I was following. I even copy-pasted the entirety of the video's code line by line and the errors still persisted, leading me to the conclusion that there is something wrong in the way Visual Studios is configured on my end, possibly due to version differences (the video was uploaded this year so it's not running on too old of a version). All my dependencies are correct as well and are present in their respective project-based directories, I have double checked those.
Lastly, the code I used above was a separate testing code I redid on a new project in an attempt to isolate the error. Needless to say, I got the same error in both cases indicating there's nothing inherently wrong with the custom-defined headers and libraries of my original project.
Upvotes: 0
Views: 417
Reputation: 249
1) You cannot assign into arrays like this once they are initialized.
int arr[3] = {1, 2, 3}; //valid
int arr2[3];
arr2 = {1, 2, 3}; //invalid
In your case, what you could do is instead of: -
TriCoord f0;
f0.Position = { x, y, z };
f0.Label = { j, 1 };
write: -
float tempFloat[3] = { x, y, z };
int tempInt[2] = { j, 1 };
TriCoord f0;
for (int i = 0; i < 3; ++i)
f0.Position[i] = tempFloat[i];
for (int i = 0; i < 2; ++i)
f0.Label[i] = tempInt[i];
2) Solve 1 to solve 2...
3) You are trying to assign std::array<TriCoord, 3>
to a float array which is certainly not possible...
At this point, I would strongly advise you to use std::array
everywhere.
Upvotes: 2