Array of pointers makes an error

S

Stephane Vollet

Can someone tell me what's wrong with my code here?
When compiling, it says :

error C2143: syntax error : missing ';' before '*'
error C2501: 'Tcase' : missing storage-class or type specifiers
error C2501: 'm_cases' : missing storage-class or type specifiers

the compiler points out the problem on this:
Tcase* m_cases[CcasesMax];

it is in the class Thoraire.

here is my code.

#include "case.h"

Tcase::Tcase(int numSemaine, int numJour, double heureDebut, double duree,
char matiere[], char classe[], char salle[]) // pas de val par défaut ici!
{
m_numSemaine = numSemaine;
m_numJour = numJour;
m_heureDebut = heureDebut;
m_duree = duree;
strcpy(m_matiere,matiere);
strcpy(m_classe,classe);
strcpy(m_salle,salle);

}
#ifndef CASE_H
#define CASE_H

#include <iostream>
#include <iomanip>
#include <cstring>

#include "horaire.h"

using namespace std;

const int Clundi=1;
const int Cmardi=2;
const int Cmercredi=3;
const int Cjeudi=4;
const int Cvendredi=5;
const int Csamedi=6;
const int Cdimanche=7;


class Tcase{

public:

Tcase(int numSemaine, int numJour, double heureDebut, double duree,
char matiere[]=" ", char classe[]=" ", char salle[]=" ");

private:

int m_numSemaine;
int m_numJour;
double m_heureDebut;
double m_duree;
char m_matiere[10];
char m_classe[10];
char m_salle[10];

};

#endif
#include "horaire.h"
#include "case.h"

Thoraire::Thoraire(const char nomEnseignant[])
{
strcpy(m_nomEnseignant,nomEnseignant);

//m_cases=NULL;

m_nbCases=0;
}

#ifndef CASE_H
#define CASE_H

#include <iostream>
#include <iomanip>
#include <cstring>

#include "case.h"

using namespace std;

class TenseignantError{};

class Toverflow{};

const int CcasesMax=1000;

class Thoraire{

public:

Thoraire(const char nomEnseignant[]);

private:

char m_nomEnseignant[30];
Tcase* m_cases[CcasesMax];
int m_nbCases;
};

#endif

// main

#include <iostream>
using namespace std;

#include "case.h"
#include "horaire.h"
int main ()
{
Tcase case1(20,1,8,1.5,"programation");
return 0;
}
 
V

Victor Bazarov

Stephane Vollet said:
Can someone tell me what's wrong with my code here?

Plenty. But I will only point out one mistake.
[..]
here is my code.
[...]
#ifndef CASE_H
#define CASE_H
[..]
#ifndef CASE_H
#define CASE_H
[..]

Notice anything strange?

V
 
S

stephane

OK thanks! I corrected it and now it compiles ok.

You send plenty of things wrong... what are the few others things wrong in
that code?

Victor Bazarov said:
Stephane Vollet said:
Can someone tell me what's wrong with my code here?

Plenty. But I will only point out one mistake.
[..]
here is my code.
[...]
#ifndef CASE_H
#define CASE_H
[..]
#ifndef CASE_H
#define CASE_H
[..]

Notice anything strange?

V
 
S

stephane

I meant:



You said there was plenty of things wrong... what are the few others things
wrong in
that code?

thank you.

stephane said:
OK thanks! I corrected it and now it compiles ok.

You send plenty of things wrong... what are the few others things wrong in
that code?

Victor Bazarov said:
Stephane Vollet said:
Can someone tell me what's wrong with my code here?

Plenty. But I will only point out one mistake.
[..]
here is my code.
[...]
#ifndef CASE_H
#define CASE_H
[..]
#ifndef CASE_H
#define CASE_H
[..]

Notice anything strange?

V
 
V

Victor Bazarov

Stephane said:
Can someone tell me what's wrong with my code here?
When compiling, it says :

error C2143: syntax error : missing ';' before '*'
error C2501: 'Tcase' : missing storage-class or type specifiers
error C2501: 'm_cases' : missing storage-class or type specifiers

the compiler points out the problem on this:
Tcase* m_cases[CcasesMax];

it is in the class Thoraire.

here is my code.

#include "case.h"

Tcase::Tcase(int numSemaine, int numJour, double heureDebut, double duree,
char matiere[], char classe[], char salle[]) // pas de val par défaut ici!

Since you asked what it was I considered wrong, here you go...

First and foremost, you should use 'std::string' instead of fixed-length
arrays of char. That will save you lots of trouble in the days ahead.

Second, prefer initialisation over assignment. Read about it in the FAQ.
{
m_numSemaine = numSemaine;
m_numJour = numJour;
m_heureDebut = heureDebut;
m_duree = duree;
strcpy(m_matiere,matiere);
strcpy(m_classe,classe);
strcpy(m_salle,salle);

Third, if somebody passes in an array longer than 10 characters, you're
screwed (this is how Windows gets broken/cracked/open). Use 'strncpy'
instead since your 'm_' arrays are only 10 characters long.
}
#ifndef CASE_H
#define CASE_H

#include <iostream>
#include <iomanip>
#include <cstring>

#include "horaire.h"

using namespace std;

Putting a 'using' directive in the header is a VERY BAD IDEA(tm). Read
about that in the archives.
const int Clundi=1;
const int Cmardi=2;
const int Cmercredi=3;
const int Cjeudi=4;
const int Cvendredi=5;
const int Csamedi=6;
const int Cdimanche=7;


class Tcase{

public:

Tcase(int numSemaine, int numJour, double heureDebut, double duree,
char matiere[]=" ", char classe[]=" ", char salle[]=" ");

If you expect to pass default pointers to literals, you should declare
your constructor as accepting pointers to const:

...
char const *matiere = " ", char const *classe = " ", char const* ..

And as you can see I dropped the '[]' and put the '*' in there. Why?
Because I don't want to kid myself that the argument is an array. It
isn't, it's just a pointer. The notation "[]" is an alternative to
pointer, and really should be avoided IMO.
private:

int m_numSemaine;
int m_numJour;
double m_heureDebut;
double m_duree;
char m_matiere[10];
char m_classe[10];
char m_salle[10];

};

#endif
#include "horaire.h"
#include "case.h"

Thoraire::Thoraire(const char nomEnseignant[])

Again, use std::string instead of arrays.
{
strcpy(m_nomEnseignant,nomEnseignant);

//m_cases=NULL;

m_nbCases=0;
}

#ifndef CASE_H
#define CASE_H

#include <iostream>
#include <iomanip>
#include <cstring>

#include "case.h"

using namespace std;

class TenseignantError{};

class Toverflow{};

const int CcasesMax=1000;

class Thoraire{

public:

Thoraire(const char nomEnseignant[]);

private:

char m_nomEnseignant[30];
Tcase* m_cases[CcasesMax];
int m_nbCases;
};

#endif

// main

#include <iostream>
using namespace std;

#include "case.h"
#include "horaire.h"
int main ()
{
Tcase case1(20,1,8,1.5,"programation");
return 0;
}

Enough for now, I guess.

V
 
S

Stephane Vollet

it's enough yeah... thanks!

Victor Bazarov said:
Stephane said:
Can someone tell me what's wrong with my code here?
When compiling, it says :

error C2143: syntax error : missing ';' before '*'
error C2501: 'Tcase' : missing storage-class or type specifiers
error C2501: 'm_cases' : missing storage-class or type specifiers

the compiler points out the problem on this:
Tcase* m_cases[CcasesMax];

it is in the class Thoraire.

here is my code.

#include "case.h"

Tcase::Tcase(int numSemaine, int numJour, double heureDebut, double duree,
char matiere[], char classe[], char salle[]) // pas de val par défaut
ici!

Since you asked what it was I considered wrong, here you go...

First and foremost, you should use 'std::string' instead of fixed-length
arrays of char. That will save you lots of trouble in the days ahead.

Second, prefer initialisation over assignment. Read about it in the FAQ.
{
m_numSemaine = numSemaine;
m_numJour = numJour;
m_heureDebut = heureDebut;
m_duree = duree;
strcpy(m_matiere,matiere);
strcpy(m_classe,classe);
strcpy(m_salle,salle);

Third, if somebody passes in an array longer than 10 characters, you're
screwed (this is how Windows gets broken/cracked/open). Use 'strncpy'
instead since your 'm_' arrays are only 10 characters long.
}
#ifndef CASE_H
#define CASE_H

#include <iostream>
#include <iomanip>
#include <cstring>

#include "horaire.h"

using namespace std;

Putting a 'using' directive in the header is a VERY BAD IDEA(tm). Read
about that in the archives.
const int Clundi=1;
const int Cmardi=2;
const int Cmercredi=3;
const int Cjeudi=4;
const int Cvendredi=5;
const int Csamedi=6;
const int Cdimanche=7;


class Tcase{

public:

Tcase(int numSemaine, int numJour, double heureDebut, double duree,
char matiere[]=" ", char classe[]=" ", char salle[]=" ");

If you expect to pass default pointers to literals, you should declare
your constructor as accepting pointers to const:

...
char const *matiere = " ", char const *classe = " ", char const* ..

And as you can see I dropped the '[]' and put the '*' in there. Why?
Because I don't want to kid myself that the argument is an array. It
isn't, it's just a pointer. The notation "[]" is an alternative to
pointer, and really should be avoided IMO.
private:

int m_numSemaine;
int m_numJour;
double m_heureDebut;
double m_duree;
char m_matiere[10];
char m_classe[10];
char m_salle[10];

};

#endif
#include "horaire.h"
#include "case.h"

Thoraire::Thoraire(const char nomEnseignant[])

Again, use std::string instead of arrays.
{
strcpy(m_nomEnseignant,nomEnseignant);

//m_cases=NULL;

m_nbCases=0;
}

#ifndef CASE_H
#define CASE_H

#include <iostream>
#include <iomanip>
#include <cstring>

#include "case.h"

using namespace std;

class TenseignantError{};

class Toverflow{};

const int CcasesMax=1000;

class Thoraire{

public:

Thoraire(const char nomEnseignant[]);

private:

char m_nomEnseignant[30];
Tcase* m_cases[CcasesMax];
int m_nbCases;
};

#endif

// main

#include <iostream>
using namespace std;

#include "case.h"
#include "horaire.h"
int main ()
{
Tcase case1(20,1,8,1.5,"programation");
return 0;
}

Enough for now, I guess.

V
 
M

Mike Wahler

Stephane Vollet said:
it's enough yeah... thanks!

A good way you can thank Victor (and all the regulars
here) for their help is to not top-post, and delete any
unnecessary text from your reply. Leave only enough
to preserve context. Thanks.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top