Struct problem??

Ok guys, this is the last time you'll see me around here. Final assignment to be turned in.

I have all my program written out and I expect it to work, except I get this compile error:

"no matching function for call to 'Book::Book.... "
etc.

I don't understand what the deal is. Does anybody recognize this?

Here is my program. Hopefully it's broken up pretty basically enough that you can skip to the important parts fairly easy.

I am very grateful to anyone who takes the time to look at my issue.


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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>
#include<string.h>

using namespace std;

struct Book
  {
  char newTitle[31];
  char newAuthor[31];
   int newCopies;
   double newPrice;
  };
  
/***************************************************************
Function: print
Use: prints the book attributes

Arguments: A string

Returns: N/A
***************************************************************/ 

void print(Book newBook)
{

cout << "Title: " << newBook.newTitle << endl << "Author: " << newBook.newAuthor << endl << 
	 "Copies in Stock: " << newBook.newCopies << endl << "Price: $" << newBook.newPrice << endl;
	 
} 

/***************************************************************
Function: setTitle
Use: changes the title for the book data

Arguments: a character array

Returns: N/A
***************************************************************/ 

void setTitle(char newTitle[], Book newBook)
{

strcpy(newBook.newTitle, newTitle);

}

/***************************************************************
Function: setAuthor
Use: changes the author for the book data

Arguments: a character array

Returns: N/A
***************************************************************/

void setAuthor(char newAuthor[], Book newBook)
{

strcpy(newBook.newAuthor, newAuthor);

}



/***************************************************************
Function: setCopies
Use: changes the number of copies in stock for the book data

Arguments: an integer representing the number of copies

Returns: N/A
***************************************************************/

void setCopies(int newCopies, Book newBook)
{

if(newCopies >= 0)
{
newBook.newCopies = newCopies;
}
else
{
newBook.newCopies = 0;

cout << endl << "Error: Copies less than zero";
}

}




/***************************************************************
Function: setPrice
Use: changes the price for the book data

Arguments: an integer representing the price

Returns: N/A
***************************************************************/

void setPrice(double newPrice, Book newBook)
{

if(newPrice >= 0)
{
newBook.newPrice = newPrice;
}
else
{
newBook.newPrice = 0;

cout << endl << "Error: Price less than zero";
}

}


/***************************************************************
Function: placeOrder
Use: Determines the outcome of an order placed for a book, whether
it be an insufficient stock, invalid order, or total for a proper
order placed

Arguments: an integer representing the quantity being ordered

Returns: N/A
***************************************************************/


void placeOrder(int orderQuantity, Book newBook)
{

int total;

total = orderQuantity * (newBook.newPrice);


if (orderQuantity <= 0)
{
cout << endl << "Invalid Order Quantity";
}
else if (orderQuantity > newBook.newCopies)
{
cout << endl << "Insufficient Stock";
}
else 
{
newBook.newCopies -= orderQuantity;

cout << endl << "Total Cost: " << total;
}

}


/***************************************************************
Function: addStock
Use: Determines the outcome of a restocking procedure taking place

Arguments: an integer representing the quantity being restocked

Returns: N/A
***************************************************************/

void addStock(int restockQuantity, Book newBook)
{

if (restockQuantity <= 0)
cout << endl << "Invalid quantity";
else
newBook.newCopies += restockQuantity;

}

/***************************************************************
Function: main
Use: runs everything else

Arguments:N/A

Returns: N/A
***************************************************************/

int main()
{

Book newBook("Soylent Green", "Andrew Evans", 31, 20);

Book fakeBook("Errors", "Rules Broken", -2, -3);

cout << endl << "Book Information" << endl;
print(newBook);
cout << endl << "Changing Book Title" << endl;
setTitle("This is a New Title", newBook);
cout << endl << "New Book Information" << endl;
print(newBook);
cout << endl << "Changing Book Author" << endl;
setAuthor("Bob Barker", newBook);
cout << endl << "New Book Information" << endl;
print(newBook);
cout << endl << "Changing Copies of Book" << endl;
setCopies(-5, newBook);
cout << endl << "New Book Information" << endl;
print(newBook);
cout << endl << "Changing Copies of Book" << endl;
setCopies(20, newBook);
cout << endl << "New Book Information" << endl;
print(newBook);
cout << endl << "Changing Book Price" << endl;
setPrice(-4, newBook);
cout << endl << "New Book Information" << endl;
print(newBook);
cout << endl << "Changing Book Price" << endl;
setPrice(25, newBook);
cout << endl << "New Book Information" << endl;
print(newBook);
cout << endl << "Placing Order" << endl;
placeOrder(99, newBook);
cout << endl << "Placing Order" << endl;
placeOrder(3, newBook);
cout << endl << "New Book Information" << endl;
print(newBook);
cout << endl << "Adding Stock" << endl;
addStock(0, newBook);
cout << endl << "Adding Stock" << endl;
addStock(12, newBook);
cout << endl << "New Book Information" << endl;
print(newBook);


return 0;

}
Since Book is a struct, you cannot create a new book the way you are. You are trying to call a Book constructor method which doesn't exist. Instead you should make Book a class, define a constructor method, and make all the other book related functions methods of the Book class.
structs may also have constructors, destructors, and methods.

I don't quite understand what you mean by making it a class... how do you define it that way?

Thanks for the help guys!
Just change the keyword struct to class, then add public: as the first line inside it.
Although as I said above, there is no need to do this.
You need to define a constructor for your struct.

Or you can just:
1
2
3
4
5
Book newbook;
strcpy(newbook.newTitle,"Soylent Green");
strcpy(newbook.newAuthor,"Andrew Evans");
newbook.newCopies=31;
newbook.newPrice=20.0;

For obvious reasons a constructor is more convenient.
Thanks guys, but I just re-read the assignment and I have to have it as a 'class' anyway- I can't use the struct alternative methods.

So jsmith: I have it turned into a class with public datatypes. I still have that same error message that I started off with though.

How do I define newBook now that I have the class like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Book
  {
  public:
  char newTitle[31];
  char newAuthor[31];
 
  int newCopies;
  
  double newPrice;
  
  
  Book(char *, char *, int, double);
  
  };


Once again, thank you a ton guys.

EDIT:

I'm calling it like this:
Book newBook("The Fabulous Life of Mr. Burns", "Andrew Evans", 31, 20.0);

And the error message is "undefined reference to Book::Book.... "

Last edited on
It's not enough with just declaring the constructor. You also need to implement it either inside the class/struct/whatever or outside.
Last edited on
I'm just kinda learning about 'class' definitions now, so I probably look silly...

I have it defined as follows now:
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
class Book
{
  public:
  char newTitle[31];
  char newAuthor[31];
  int newCopies;
  double newPrice;
  
  
  public:
  Book(char newTitle[], char newAuthor[], int newCopies, double newPrice)
  {
   
    strcpy(this.newTitle, newTitle);
	strcpy(this.newAuthor, newAuthor);
	this.newCopies = newCopies;
	this.newPrice = newPrice;
  }

};
  
  
public class newBook
{

  public static void Main()
  {

    // create a Book object using the constructor
    // defined in the class
    Book newBook = new Book("The Fabulous Life of Mr. Burns", "Andrew Evans", 14, 20.20);

    

  }

}


But I get errors for the 'this' lines

"... is of non-class type 'Book* const' ".

How am I supposed to do those properly? I had it as 'newBook.??' before but newBook isn't defined until later. Some google search showed me the same kind of declaration using 'this', but it isn't working.

There's also a random "expected unqualified-id before "public" appearing in line 23 there :S
Last edited on
This is a pointer so use this->/*member*/, not this./*member*/

PS: If you are using only public members, the struct allows you less typing but it is the same as a class ( http://www.cplusplus.com/forum/beginner/5980/ )
Last edited on
Wait... That's C#!
Last edited on
Topic archived. No new replies allowed.