The second parameter in the class definition is an Array

I am not sure how to write in in my main()
I get this error: [Error] C:\Martin\MalikChapter1\bookType23032016.cpp:166: error: expected primary-expression before '[' token

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <iostream>
#include <string>
	using namespace std;

	class BookType{

	private:

	     string Title;
	    string Authors[4];
        string publisher;
	    int ISBN;
	    double price;
	    int NumOfCopies;
	    int AnotherMember;

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

Please help
Method one (very bad):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <limits>
#include <string>

class BookType {
public:
    BookType(std::string title_arg     , std::string authors_arg[4],
             std::string publisher_arg , int isbn_arg,
             double price_arg          , int num_copies_arg,
             int another_member_arg)
        : title          { title_arg }    , authors    { authors_arg[0],
                                                         authors_arg[1],
                                                         authors_arg[2],
                                                         authors_arg[3]  },
          publisher      { publisher_arg} , isbn       { isbn_arg},
          price          { price_arg }    , num_copies { num_copies_arg },
          another_member { another_member_arg }
        {}
private:
    std::string title;
    std::string authors[4];
    std::string publisher;
    int isbn;
    double price;
    int num_copies;
    int another_member;
    friend std::ostream& operator<<(std::ostream& os, const BookType& rhs)
    {
        os << "title: " << rhs.title << "; authors: { ";
        for(int i{}; i<4; ++i) { os << rhs.authors[i] << ' '; }
        os << "};\npublisher: " << rhs.publisher << "; isbn: " << rhs.isbn
           << "; copies: " << rhs.num_copies << "; and also: " << rhs.another_member;
        return os;
    }
};

void waitForEnter();

int main()
{
    BookType book2 { "Sweet", new std::string[4] { "lil", "lool", "lal", "ln" },
                     "ssss", 2, 12292883.47, 8, 3 };
    std::cout << book2 << '\n';
    waitForEnter();
    return 0;   
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

title: Sweet; authors: { lil lool lal ln };
publisher: ssss; isbn: 2; copies: 8; and also: 3

Press ENTER to continue...


Method two (bad):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <limits>
#include <string>

class BookType {
public:
    BookType(std::string title_arg     , std::string* authors_arg,
             std::string publisher_arg , int isbn_arg,
             double price_arg          , int num_copies_arg,
             int another_member_arg)
        : title          { title_arg }    , authors    { authors_arg },
          publisher      { publisher_arg} , isbn       { isbn_arg},
          price          { price_arg }    , num_copies { num_copies_arg },
          another_member { another_member_arg }
        {}
private:
    std::string title;
    std::string* authors;
    std::string publisher;
    int isbn;
    double price;
    int num_copies;
    int another_member;
    friend std::ostream& operator<<(std::ostream& os, const BookType& rhs)
    {
        os << "title: " << rhs.title << "; authors: { ";
        for(int i{}; i<4; ++i) { os << rhs.authors[i] << ' '; }
        os << "};\npublisher: " << rhs.publisher << "; isbn: " << rhs.isbn
           << "; copies: " << rhs.num_copies << "; and also: " << rhs.another_member;
        return os;
    }
};

void waitForEnter();

int main()
{
    BookType book2 { "Sweet", new std::string[4] { "lil", "lool", "lal", "ln" },
                     "ssss", 2, 12292883.47, 8, 3 };
    std::cout << book2 << '\n';
    waitForEnter();
    return 0;   
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


Method three (recommended):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <limits>
#include <string>
#include <vector>

class BookType {
public:
    BookType(std::string title_arg     , std::vector<std::string> authors_arg,
             std::string publisher_arg , int isbn_arg,
             double price_arg          , int num_copies_arg,
             int another_member_arg)
        : title          { title_arg }    , authors    { authors_arg },
          publisher      { publisher_arg} , isbn       { isbn_arg},
          price          { price_arg }    , num_copies { num_copies_arg },
          another_member { another_member_arg }
        {}
private:
    std::string title;
    std::vector<std::string> authors;
    std::string publisher;
    int isbn;
    double price;
    int num_copies;
    int another_member;
    friend std::ostream& operator<<(std::ostream& os, const BookType& rhs)
    {
        os << "title: " << rhs.title << "; authors: { ";
        for(const auto& s: rhs.authors) { os << s << ' '; }
        os << "};\npublisher: " << rhs.publisher << "; isbn: " << rhs.isbn
           << "; copies: " << rhs.num_copies << "; and also: " << rhs.another_member;
        return os;
    }
};

void waitForEnter();

int main()
{
    BookType book2 { "Sweet", { "lil", "lool", "lal", "ln" },
                     "ssss", 2, 12292883.47, 8, 3 };
    std::cout << book2 << '\n';
    waitForEnter();
    return 0;   
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Something I experimented with, it could probably be improved. I used a simplified version of the class here for this illustration.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <initializer_list>
#include <algorithm>	

using namespace std;

class BookType {

private:

    string Title;
    string Authors[4];

public:
    BookType() {}

    BookType(string title, std::initializer_list<std::string> authors)
        : Title(title)
    {
        if (authors.size() <= 4)
            std::copy( authors.begin(), authors.end(), Authors ) ;
        else
            std::copy( authors.begin(), authors.begin() + 4, Authors ) ;
            
    }
    
    friend std::ostream & operator << (  std::ostream & os, const BookType& b);
        
};


std::ostream & operator << (  std::ostream & os, const BookType& b)
{
    os << "Title:   " << b.Title << '\n';
    os << "Authors: " << b.Authors[0];
    for (int i=1; i<4; ++i)
    {
        if (b.Authors[i].size())
            os << ", " << b.Authors[i]; 
    }
    os << '\n';
    return os;
}


int main()
{    
    BookType Book2("Sweet", {"lil", "lool", "ln"} );
    cout << "Books2 = \n" << Book2;
    
    BookType Book3("Sweet", {"lil", "lal", "ln", "x", "y", "z", "a", "b", "c", "d" } );
    cout << "Books3 = \n" << Book3;
}

Books2 =
Title:   Sweet
Authors: lil, lool, ln
Books3 =
Title:   Sweet
Authors: lil, lal, ln, x

I am using a C-Free 5.0 IDE and when I try to run the code that Chevril gave above I get this error on line 3 #include<initializer_list>:
[Error] C:\Dev-Cpp\MalikChapter3\Example3-5\ProjExample3-5\StructS.cpp:3:28: initializer_list: No such file or directory
The std::initializer_list and its corresponding header #include <initializer_list> were introduced into the language as part of C++11.

I'm not familiar with the C-Free 5.0 IDE you mention, however it seems that either it does not use C++11 (or later) compiler, or if it does, the compiler may be configured to use an older standard - probably C++98.

edit:
On the other hand, the same code (which still requires C++11) can be modified to use a std::vector instead of the std::initializer_list and it should work:


change line 3:
 
#include <initializer_list> 
to
 
#include <vector> 


and then change line 18
 
    BookType(string title, std::initializer_list<std::string> authors)
to
 
    BookType(string title, std::vector<std::string> authors)



Last edited on
Topic archived. No new replies allowed.