-2

In the following code :

#include<iostream>
#include <string>
using namespace std;

class BookType{

private:

    string Title;
    string Authors[4];
    string Authors;
}

Here is the main program:

int main() {
    BookType Book2("Sweet",["lil,lool,lal,ln"],"ssss",29288308.47,8,3,2);
}

I get this error: expected primary-expression before '[' token

Yash
  • 11,486
  • 4
  • 19
  • 35
Bopaki
  • 35
  • 1
  • 5
  • 4
    I suggest you [get a couple of good beginners books to read](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Because your use of square brackets `[` is not the only error in the code you show. – Some programmer dude Nov 15 '17 at 09:44
  • 1
    You have duplicate variable names `string Authors[4];` and `string Authors;`, change it – Ivan Sheihets Nov 15 '17 at 09:48
  • [Usual link for why `using namespace std` should never, ever be used](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Quentin Nov 15 '17 at 10:16

1 Answers1

0

You are calling the constructor of BookType but you didn't define one. Here is an example

#include <iostream>
#include <string>
#include <vector>
#include <initializer_list>

class BookType {
public:
    BookType(std::string s1, std::initializer_list<std::string> l, std::string s2, double d, int i1, int i2, int i3): title(s1), authors(l) {}

private:

    std::string title;
    std::vector<std::string> authors;
    std::string Authors;
};

int main() {
    BookType Book2("Sweet",{"lil","lool","lal","ln"},"ssss",29288308.47,8,3,2);
    return 0;
}

I added the constructor

BookType(std::string s1, std::initializer_list<std::string> l, std::string s2, double d, int i1, int i2, int i3): title(s1), authors(l) {}

and changed your array of Authors into a std::vector of authors. std::vectors are easier to handle and can be initialized with an std::initializer_list. Also a std::initializer_list is passed to the constructor. ["lil,lool,lal,ln"] is no array.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62