0
struct article {
    int uId;
    char uName[201];

    article(int Id, char* name) { uId = Id;
        for(int i=0;name[i]!='\0';i++)
        {
            uName[i]=name[i];
        }

    }
};

int main()
{
char Name[201];
int ID;
int N;
cin>>N;
article* articleArr = new article[N];  //error thrown on this line
/*Some code that creates ID and Name*/
articleArr[index] = article(Id, Name);
cout<<articleArr[index].uId<<' '<<articleArr[index].uName<<endl;


}

The problem I have with my code is that I can't dinamically create an array of structs. Compiler throws out an error "no matching function for call to 'article::article()" at the (article* articleArr = new article[N];) line. Before I started implementing dynamic initialization of array of structs it worked fine. I have to use char arrays, not allowed to use strings.

combat
  • 3
  • 2
  • 1
    You need to add a default constructor (i.e. without parameters). – HolyBlackCat May 04 '22 at 22:07
  • My *real* advice is "don't use `char*` and `[]`; use `std::string` and `std::vector`", but since your teacher is already stated to be pathologically opposed to programming constructs invented in the past century, my second-best advice is to carefully read the above link. – Silvio Mayolo May 04 '22 at 22:09
  • 2
    @Silvio Show some kindness. The teacher can't reuse the lesson plan from the C programming course (which reuses the lesson plan from the Pascal course (which reused the the lesson plan from the Fortran course (which reused the lesson plan from the COBOL course (which reused the lesson plan from the Antikythera course (...))))))) if you use new constructs. You can't expect these folks to actually work, can you? – user4581301 May 04 '22 at 22:15
  • 1
    And the cruel thing is that `std::vector` and `std::string`, as of this year (2022), is officially **24 years old**. It isn't something new that came about a year or so ago -- why teachers still think it's some new-fangled invention is beyond me at this point. – PaulMcKenzie May 04 '22 at 22:35

2 Answers2

0

This line

article* articleArr = new article[N];  

Is trying to invoke the default constructor but you do not have one.

You need to add

struct article {
    int uId;
    char uName[201];
    article() {  
         /// whatever code is needed to initalize an empty article
    }
    article(int Id, char* name) { uId = Id;
        for(int i=0;name[i]!='\0';i++)
        {
            uName[i]=name[i];
        }

    }
};

c++ would normally automatically do this for yo , but because you gave a constructor c++ did not create any others

BTW - you should really use std::vector rather that a 'new'ed array, its much easier and safer to use

same is true for std::string instead of naked char *

pm100
  • 48,078
  • 23
  • 82
  • 145
0
article(int Id, char* name)

You have declared a constructor that accepts parameters. As a consequence, the class will not have an implicitly generated default constructor. Since you haven't defined a constructor either, the class is not default constructible.

new article[N]`

This default constructs an array of N instances of the class article. This is ill-formed because the class is not default constructible.

Potential solutions:

  • Define a default constructor.
  • Or don't default construct instances of the class.
eerorika
  • 232,697
  • 12
  • 197
  • 326