By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

weird problem with gcc

P: n/a
I hope I'm not annoying anyone here with this kind of newbie question,
anyway here it is:
What is wrong with this code here?

const int ARRAY_SIZE = 10;

gcc says: error: two or more data types in declaration of 'ARRAY_SIZE'

I've checked a few other sites and that confirmed that there isn't
really anything wrong with it at first glance.
This happened on a Mac with OS X 10.4.3 and this version of gcc:
powerpc-apple-darwin8-gcc-4.0.0
and also on a i586 running SuSE 10.

Thanks in advance for your help

Dec 16 '05 #1
Share this Question
Share on Google+
22 Replies


P: n/a
mini_t wrote:
I hope I'm not annoying anyone here with this kind of newbie question,
anyway here it is:
What is wrong with this code here?

const int ARRAY_SIZE = 10;

gcc says: error: two or more data types in declaration of 'ARRAY_SIZE'

I've checked a few other sites and that confirmed that there isn't
really anything wrong with it at first glance.
This happened on a Mac with OS X 10.4.3 and this version of gcc:
powerpc-apple-darwin8-gcc-4.0.0
and also on a i586 running SuSE 10.

Thanks in advance for your help


There's nothing wrong with it on the surface. Post a minimal *but
complete* code sample that demonstrates the problem.

Cheers! --M

Dec 16 '05 #2

P: n/a

mini_t wrote:
I hope I'm not annoying anyone here with this kind of newbie question,
anyway here it is:
What is wrong with this code here?

const int ARRAY_SIZE = 10;

gcc says: error: two or more data types in declaration of 'ARRAY_SIZE'

I've checked a few other sites and that confirmed that there isn't
really anything wrong with it at first glance.
This happened on a Mac with OS X 10.4.3 and this version of gcc:
powerpc-apple-darwin8-gcc-4.0.0
and also on a i586 running SuSE 10.


it looks ok to me. So if wierd things seem to be happening then suspect
the
preprocessor. Try

const int array_size = 10;

in general don't use all-uppercase for anything except macros
--
Nick Keighley

Dec 16 '05 #3

P: n/a
mini_t wrote:
I hope I'm not annoying anyone here with this kind of newbie question,
anyway here it is:
What is wrong with this code here?

const int ARRAY_SIZE = 10;

gcc says: error: two or more data types in declaration of 'ARRAY_SIZE'
Look at the previous line, maybe you're missing a semicolon there.
I've checked a few other sites and that confirmed that there isn't
really anything wrong with it at first glance.
This happened on a Mac with OS X 10.4.3 and this version of gcc:
powerpc-apple-darwin8-gcc-4.0.0
and also on a i586 running SuSE 10.


Next time begin with reading the FAQ. Your assignment today is 5.8.

V
Dec 16 '05 #4

P: n/a
mini_t <se*************@gmail.com> wrote:
I hope I'm not annoying anyone here with this kind of newbie question,
anyway here it is:
What is wrong with this code here?

const int ARRAY_SIZE = 10;

gcc says: error: two or more data types in declaration of 'ARRAY_SIZE'

I've checked a few other sites and that confirmed that there isn't
really anything wrong with it at first glance.
This happened on a Mac with OS X 10.4.3 and this version of gcc:
powerpc-apple-darwin8-gcc-4.0.0
and also on a i586 running SuSE 10.

Thanks in advance for your help


My first inclination is that ARRAY_SIZE is some macro that is #define'd
somewhere.

--
Marcus Kwok
Dec 16 '05 #5

P: n/a
Marcus Kwok wrote:
mini_t <se*************@gmail.com> wrote:
I hope I'm not annoying anyone here with this kind of newbie question,
anyway here it is:
What is wrong with this code here?

const int ARRAY_SIZE = 10;

gcc says: error: two or more data types in declaration of 'ARRAY_SIZE'

I've checked a few other sites and that confirmed that there isn't
really anything wrong with it at first glance.
This happened on a Mac with OS X 10.4.3 and this version of gcc:
powerpc-apple-darwin8-gcc-4.0.0
and also on a i586 running SuSE 10.

Thanks in advance for your help

My first inclination is that ARRAY_SIZE is some macro that is #define'd
somewhere.


If it were true, the error wouldn't have 'ARRAY_SIZE' spelled out, it
would have the substituted text there, I think.

V
Dec 16 '05 #6

P: n/a
ok, here's the way the code is in the program (it's actually part of a
c++ course i'm doing):

#include <iostream>
#include <iomanip>
using namespace std;
#include "Salaried.h"
const int array_size = 10; // maximum number of employees
// function prototypes
int createEmployee( SalariedEmployee * [], int );
void displayEmployees( SalariedEmployee * [], int );
int main()
{
int count = 0;
int selection = 0;
cout << fixed << setprecision( 2 );
SalariedEmployee *employees[ array_size ];

Dec 16 '05 #7

P: n/a
mini_t wrote:
ok, here's the way the code is in the program (it's actually part of a
c++ course i'm doing):

#include <iostream>
#include <iomanip>
using namespace std;
#include "Salaried.h"
const int array_size = 10; // maximum number of employees
// function prototypes
int createEmployee( SalariedEmployee * [], int );
void displayEmployees( SalariedEmployee * [], int );
int main()
{
int count = 0;
int selection = 0;
cout << fixed << setprecision( 2 );
SalariedEmployee *employees[ array_size ];


We'll need to see the content of Salaried.h, too. That's likely where
the problem is.

BTW, you should prefer std::vector to standard arrays. See this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-34.1

Cheers! --M

Dec 16 '05 #8

P: n/a
thanks for the advice about arrays, here's Salaried.h
#ifndef SALARIED_H
#define SALARIED_H

#include "Employee.h" // Employee class definition

class SalariedEmployee : public Employee {

public:
SalariedEmployee( string &, string &, string &, double
= 0.0 );

void setWeeklySalary( double );
double getWeeklySalary();

void print(); // displays "Salaried employee: "

private:
double weeklySalary;

} // end class SalariedEmployee
#endif // SALARIED_H
and Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string> // required to access string functions

using namespace std; // for accessing C++ Standard Library members

class Employee {

public:
Employee( string &, string &, string & );

void setFirstName( string & );
string getFirstName();

void setLastName( string & );
string getLastName();

void setSocialSecurityNumber( string & );
string getSocialSecurityNumber();

// member function to be overridden
void print(); // display employee information

private:
string firstName;
string lastName;
string socialSecurityNumber;

}; // end class Employee

#endif // EMPLOYEE_H

Dec 16 '05 #9

P: n/a
mini_t wrote:
ok, here's the way the code is in the program (it's actually part of a
c++ course i'm doing):

#include <iostream>
#include <iomanip>
using namespace std;
#include "Salaried.h"
What's inside this one? Does it have a class definition that ends on
a curly brace without the closing semicolon?
const int array_size = 10; // maximum number of employees
// function prototypes
int createEmployee( SalariedEmployee * [], int );
void displayEmployees( SalariedEmployee * [], int );
int main()
{
int count = 0;
int selection = 0;
cout << fixed << setprecision( 2 );
SalariedEmployee *employees[ array_size ];


Where is the end of 'main'? Do you understand the concept of posting
_complete_ code? Yes, the rest of 'main' is probably irrelevant, but
you are supposed to have at least the closing curly brace there, no?

V
Dec 16 '05 #10

P: n/a
mini_t wrote:
thanks for the advice about arrays, here's Salaried.h
#ifndef SALARIED_H
#define SALARIED_H

#include "Employee.h" // Employee class definition

class SalariedEmployee : public Employee {

public:
SalariedEmployee( string &, string &, string &, double
= 0.0 );

void setWeeklySalary( double );
double getWeeklySalary();

void print(); // displays "Salaried employee: "

private:
double weeklySalary;

} // end class SalariedEmployee
^^^^

Just like I said. A class definition _ends_ with a _semicolon_ !
#endif // SALARIED_H
and Employee.h
[...]

Dec 16 '05 #11

P: n/a
thanks, I have to stop relying on the template code that comes with my
course

Dec 16 '05 #12

P: n/a
On 16 Dec 2005 07:03:16 -0800, "mini_t" <se*************@gmail.com>
wrote:
I hope I'm not annoying anyone here with this kind of newbie question,
anyway here it is:
What is wrong with this code here?

const int ARRAY_SIZE = 10;

gcc says: error: two or more data types in declaration of 'ARRAY_SIZE'

I've checked a few other sites and that confirmed that there isn't
really anything wrong with it at first glance.
This happened on a Mac with OS X 10.4.3 and this version of gcc:
powerpc-apple-darwin8-gcc-4.0.0
and also on a i586 running SuSE 10.

Thanks in advance for your help

Just for the record:

There are some compiler specific headers which have a line similar to
the following:

#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0])

This might be the source of your problem.

Try to use another name for your array size.

regards,

-- Zara
Dec 16 '05 #13

P: n/a
mini_t wrote:
thanks for the advice about arrays, here's Salaried.h
[redacted]
and Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string> // required to access string functions

using namespace std; // for accessing C++ Standard Library members
Never, EVER do this. Never put "using namespace std;" in a header file.
If you need to use namespace std, the put it in your .cpp file.
[redacted]
#endif // EMPLOYEE_H

Dec 16 '05 #14

P: n/a
On 16 Dec 2005 08:19:10 -0800, "mini_t" <se*************@gmail.com>
wrote:
thanks, I have to stop relying on the template code that comes with my
course


If your course gives example code like that, it's at least 8 years out
of date. Buy a good book, and only write code like that to appease
your instructors and get a good grade.
Dec 16 '05 #15

P: n/a
Victor Bazarov <v.********@comacast.net> wrote:
Marcus Kwok wrote:
mini_t <se*************@gmail.com> wrote:
const int ARRAY_SIZE = 10;

gcc says: error: two or more data types in declaration of 'ARRAY_SIZE'


My first inclination is that ARRAY_SIZE is some macro that is #define'd
somewhere.


If it were true, the error wouldn't have 'ARRAY_SIZE' spelled out, it
would have the substituted text there, I think.


Oh, you're probably right, as the preprocessor should be invoked before
the compiler.

--
Marcus Kwok
Dec 16 '05 #16

P: n/a

Nick Keighley wrote:
[snip]

it looks ok to me. So if wierd things seem to be happening then suspect
the
preprocessor. Try


In my experience, I have never found a bug in the preprocessor nor in
the compiler. Every time my code (or the compilation of my code)
presented surprising results, it was my fault. Thus I try to find the
mistake among my own writings (and it is allways there) before I start
to think the problem is in the compiler.
If you can't spot the problem and still suspect the problem is the
compiler, try another compiler before doing "magic" with your code.
Magic is never the answer.

HTH,

Marcelo Pinto

Dec 16 '05 #17

P: n/a

Marcelo Pinto wrote:
Nick Keighley wrote:
[snip]

it looks ok to me. So if wierd things seem to be happening then suspect
the
preprocessor. Try


In my experience, I have never found a bug in the preprocessor nor in
the compiler. Every time my code (or the compilation of my code)
presented surprising results, it was my fault. Thus I try to find the
mistake among my own writings (and it is allways there) before I start
to think the problem is in the compiler.
If you can't spot the problem and still suspect the problem is the
compiler, try another compiler before doing "magic" with your code.
Magic is never the answer.


It possible that by "suspect the preprocessor", Nick Keighly did not
mean "suspect the preprocessor has a bug and is responsible for your
problem" but instead meant "suspect the preprocessor is performing
precisely as intended and somehow mangling your code so that what you
see on the screen and what the compiler sees are not the same thing"

That's how I read it.

Gavin Deane

Dec 17 '05 #18

P: n/a
de*********@hotmail.com wrote:
Marcelo Pinto wrote:
Nick Keighley wrote:
it looks ok to me. So if wierd things seem to be happening then suspect
the preprocessor. Try


In my experience, I have never found a bug in the preprocessor nor in
the compiler.
actually I *have* encountered compiler bugs. Though admittedly very
very rarely.

Every time my code (or the compilation of my code)
presented surprising results, it was my fault. Thus I try to find the
mistake among my own writings (and it is allways there) before I start
to think the problem is in the compiler.
indeed this is my default position
If you can't spot the problem and still suspect the problem is the
compiler, try another compiler before doing "magic" with your code.
Magic is never the answer.


It possible that by "suspect the preprocessor", Nick Keighly did not
mean "suspect the preprocessor has a bug and is responsible for your
problem" but instead meant "suspect the preprocessor is performing
precisely as intended and somehow mangling your code so that what you
see on the screen and what the compiler sees are not the same thing"

That's how I read it.


yes, that is what I meant. Else-thread I have been shown to be wrong.
--
Nick Keighley

Dec 17 '05 #19

P: n/a

red floyd wrote:
using namespace std; // for accessing C++ Standard Library members


Never, EVER do this. Never put "using namespace std;" in a header file.
If you need to use namespace std, the put it in your .cpp file.


Why not? (I'm not arguing -- I just believe that the reasons would be
interesting (to me).)

I don't understand the difference in meaning between .h files and .cpp
files although this lack of understanding hasn't (so far) affected my
ability to program. I put class definitions and function prototypes in
..h files and everything else in .cpp files. But that doesn't tell me
what the conceptual difference is. Yes, I know I could do a google
search and resolve all this. But since you did give an injunction:
"Never, EVER do this.", the onus is arguably on you to also give
reasons for following your advice.

Paul Epstein

Dec 17 '05 #20

P: n/a

pa**********@att.net wrote in message
<11*********************@z14g2000cwz.googlegroups. com>...

red floyd wrote:
> using namespace std; // for accessing C++ Standard Library members


Never, EVER do this. Never put "using namespace std;" in a header file.
If you need to use namespace std, then put it in your .cpp file.


Why not? (I'm not arguing -- I just believe that the reasons would be
interesting (to me).)

I don't understand the difference in meaning between .h files and .cpp
files although this lack of understanding hasn't (so far) affected my
ability to program. I put class definitions and function prototypes in
.h files and everything else in .cpp files. But that doesn't tell me
what the conceptual difference is. Yes, I know I could do a google
search and resolve all this. But since you did give an injunction:
"Never, EVER do this.", the onus is arguably on you to also give
reasons for following your advice.
Paul Epstein


#include <iostream>
#include <ostream>
#include <sstream>
// using namespace std;
std::ostringstream cout;

int main(){
int aNum(43);
cout << aNum << std::endl;
std::cout << cout.str() <<std::endl;
return 0;
}

Now uncomment the 'using *' line and see what your compiler tells you. Yeah,
you could use a different name for the stringstream, and go change it in the
million-plus lines in the project that used it, BUT, why should you have to
do that!?!?!

If you put 'using namespace std;' in a header, and that header gets included
in other headers/TUs in the project, you open the std space in the whole
project. Some programmer in Portland will be calling another project
programmer in Maine with many bad words because his code just got trashed.

Do you know **every** word/name in the std namespace? Avoid name clashes,
don't use 'using' in that way.

See the FAQ(s) for more.
--
Bob R
POVrookie
Dec 17 '05 #21

P: n/a

Nick Keighley wrote:
It possible that by "suspect the preprocessor", Nick Keighly did not
mean "suspect the preprocessor has a bug and is responsible for your
problem" but instead meant "suspect the preprocessor is performing
precisely as intended and somehow mangling your code so that what you
see on the screen and what the compiler sees are not the same thing"

That's how I read it.


yes, that is what I meant. Else-thread I have been shown to be wrong.
--
Nick Keighley


I misunderstood your message. My fault. :(

Marcelo Pinto

Dec 19 '05 #22

P: n/a
Marcelo Pinto wrote:
Nick Keighley wrote:

It possible that by "suspect the preprocessor", Nick Keighly did not
mean "suspect the preprocessor has a bug and is responsible for your
problem" but instead meant "suspect the preprocessor is performing
precisely as intended and somehow mangling your code so that what you
see on the screen and what the compiler sees are not the same thing"

That's how I read it.


yes, that is what I meant. Else-thread I have been shown to be wrong.


I misunderstood your message. My fault. :(


<shrug> no problem, I probably could have been clearer
--
Nick Keighley

Dec 19 '05 #23

This discussion thread is closed

Replies have been disabled for this discussion.