Expected constructor destructor or type conversion before token lỗi năm 2024

error: expected constructor, destructor, or type conversion...

I have some C++ code I am working on that receives the following compilation error:

Code:

game.cpp:29:11: error: expected constructor, destructor, or type conversion before �(� token Game::Game(date, true, boxCount)

I cannot seem to find the reason this is occurring, because this is how a constructor is supposed to be. Any advice would be greatly appreciated.

box.h

Code:

ifndef BOX_H

define BOX_H

include

class Box { friend std::ostream& operator<<(std::ostream& o, const Box& b); friend bool operator<(const int i, const Box& b); friend bool operator==(const int i, const Box& b); friend int operator+(const int i, const Box& b); public: Box(std::string d, bool f, int bc); bool operator<(const Box& b) const; protected: std::string date; int productCount; int boxCount; bool isFull;

box.cpp

Code:

include

include

include "box.h"

using namespace std; Box::Box(string d, bool f, int bc) :date(d) ,boxCount(bc) ,productCount() ,isFull(f) { } ostream& operator<<(ostream& o, const Box& b) { return o << "Box[" << b.productCount << "]"; } bool operator<(const int i, const Box& b) { return i < b.productCount; } bool operator==(const int i, const Box& b) { return i == b.productCount; } int operator+(const int i, const Box& b) { return i + b.productCount; } bool Box::operator<(const Box& b) const { return productCount < b.productCount; }

game.h

Code:

ifndef GAME_H

define GAME_H

include

include "box.h"

class Game : public Box { friend std::ostream& operator<<(std::ostream& o, const Game& b); friend bool operator<(const int i, const Game& b); friend bool operator==(const int i, const Game& b); friend int operator+(const int i, const Game& b); public: Game(std::string, bool, int); };

endif

game.cpp

Code:

include

include

include "game.h"

include "box.h"

using namespace std; Game::Game(date, true, boxCount) :Box(date, true, boxCount) ,productCount(6) { }

Somebody please help, because I have never seen this error before and can't see what I am doing wrong.

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
//Orbital Data

# include 

# include 

# include 

# include 

# include 
using namespace std;

# define PI 3.141592
//r=a+e(r*cos(p))
//find a and e using method of least squares
int main() {
 double r[50],phi[50],rphi[50],ravg,rphiavg,a,e,cov,v;
 int j,k;
 ifstream orbitdata;
 orbitdata.open ("orbits.dat");
  if (!orbitdata) {
 cerr<< "error:file will not open"<< endl;
 exit(1);
 }
 k=0;
 orbitdata >> phi[0] >> r[0];
 while (orbitdata.good()){
   k++;
   orbitdata >> phi[k] >> r[k];
 }
 for (j=0;j

Don't just read the error messages, inspect them.

You had an extra } token that ended main before return 0; This is why I use this style:

Here's your code. Also, I think we shouldn't use *.h lib files, but instead c* as in cmath instead of math.h

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

# include 

# include 

# include 

# include 

# include 
using namespace std;

# define PI 3.141592
//r=a+e(r*cos(p))
//find a and e using method of least squares
int main() {
 double r[50],phi[50],rphi[50],ravg,rphiavg,a,e,cov,v;
 int j,k;
 ifstream orbitdata;
 orbitdata.open ("orbits.dat");
  if (!orbitdata) {
 cerr<< "error:file will not open"<< endl;
 exit(1);
 }
 k=0;
 orbitdata >> phi[0] >> r[0];
 while (orbitdata.good()){
   k++;
   orbitdata >> phi[k] >> r[k];
 }
 for (j=0;j

thank you, I just needed a fresh pair of eyes to look at it. I will take your suggestion into account.