Question

I am getting a very weird error when trying to compile a simple linked list that stores "Participants". However I am clearly giving it the correct data, that can be confirmed because it works when not using initialization lists (doing a assign operator "=").

Bellow are the relevant code blocks:

struct Participant
{
    unsigned startNumber;
    std::string forename;
    std::string surname;
    std::string club;
    float finishTime;
};

struct ListNode
{
    ListNode* next;
    Participant data;
};

Participant tempData {list->data}; //error occurs here.

Below is the error i get:

List.cpp:15:36: error: cannot convert ‘Participant’ to ‘unsigned int’ in initialization

Replacing:

Participant tempData {list->data};

With:

Participant tempData = list->data;

Compiles and runs smoothly.

g++ (Debian 4.7.2-5) 4.7.2
CrunchBang (Debian 7 'Wheezy')

Any ideas on what the problem is? Any help is greatly appreciated!

Best Regards, Erik Jansson

Was it helpful?

Solution

You're trying to perform aggregate initialisation of a Participant, where the first element is an unsigned int. Naturally the one argument you give in that initialisation list is not a match for that initialisation.

I'd say this is a good example of why switching to C++11-style {} initialisation absolutely everywhere for no reason whatsoever is error-prone.

What is wrong with:

Participant tempData = list->data;

or:

Participant tempData(list->data);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top