Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with scope

Florian Lindner
Guest
 
Posts: n/a
#1: Aug 21 '06
Hello,
I have these class declarations

class CBauteil {
private:
int m_iNumber;
int m_iWeight;
public:
int getNumber();
void setNumber(int);
void setWeight(int);
void printInfo();
};

class CDINBauteil : public CBauteil {
private:
int m_iDINNumber;
public:
int getDINNumber();
void setDINNumber(int);
void printDINInfo();
};

They are located in their own header file.

In the CPP file:

[...includes as well as definition of CBauteil...]


int CDINBauteil::getDINNumber()
{
return m_iDINNumber; // <--
}

void CDINBauteil::setDINNumber(int newDIN)
{
m_iDINNumber = newDIN; // <--
}

void CDINBauteil::printDINInfo()
{
cout << "Norm: DIN " << m_iDINNumber << endl; // <--
}

this gives an error:

florian@horus ~/Dokumente/Studium/SE/SE9 $ g++ classes.cpp
classes.cpp: In member function `int CDINBauteil::getDINNumber()':
classes.cpp:28: error: `m_iDINNumber' was not declared in this scope
classes.cpp: In member function `void CDINBauteil::setDINNumber(int)':
classes.cpp:33: error: `m_iDINNumber' was not declared in this scope
classes.cpp: In member function `void CDINBauteil::printDINInfo()':
classes.cpp:38: error: `m_iDINNumber' was not declared in this scope

at the marked lines.

Why are these errors? m_iDINNumber is part of CDINBauteil, isn't?

Thanks,

Flo

Howard
Guest
 
Posts: n/a
#2: Aug 21 '06

re: Problem with scope



"Florian Lindner" <Florian.Lindner@xgm.dewrote in message
news:eccjb1$f9g$1@news.in.tum.de...
Quote:
Hello,
I have these class declarations
>
class CBauteil {
private:
int m_iNumber;
int m_iWeight;
public:
int getNumber();
void setNumber(int);
void setWeight(int);
void printInfo();
};
>
class CDINBauteil : public CBauteil {
private:
int m_iDINNumber;
public:
int getDINNumber();
void setDINNumber(int);
void printDINInfo();
};
>
They are located in their own header file.
>
In the CPP file:
>
[...includes as well as definition of CBauteil...]
What does that mean?
Quote:
>
>
int CDINBauteil::getDINNumber()
{
return m_iDINNumber; // <--
}
>
void CDINBauteil::setDINNumber(int newDIN)
{
m_iDINNumber = newDIN; // <--
}
>
void CDINBauteil::printDINInfo()
{
cout << "Norm: DIN " << m_iDINNumber << endl; // <--
}
>
this gives an error:
>
florian@horus ~/Dokumente/Studium/SE/SE9 $ g++ classes.cpp
classes.cpp: In member function `int CDINBauteil::getDINNumber()':
classes.cpp:28: error: `m_iDINNumber' was not declared in this scope
classes.cpp: In member function `void CDINBauteil::setDINNumber(int)':
classes.cpp:33: error: `m_iDINNumber' was not declared in this scope
classes.cpp: In member function `void CDINBauteil::printDINInfo()':
classes.cpp:38: error: `m_iDINNumber' was not declared in this scope
>
at the marked lines.
>
Why are these errors? m_iDINNumber is part of CDINBauteil, isn't?
>
Are there other errors as well? Perhaps there is an error in the header
file which defines the CDINBauteil class. If so, then the class isn't
defined yet, and you might get this kind of error.

Or, perhaps you're not including the header file(s) correctly? Perhaps a
misspelled filename in the #include statement(s)? You haven't shown that
#include anywhere, or told us the name(s) of the file(s) that those classes
are defined in, so we can't tell.

-Howard




Florian Lindner
Guest
 
Posts: n/a
#3: Aug 21 '06

re: Problem with scope


Howard wrote:
Quote:
Or, perhaps you're not including the header file(s) correctly? Perhaps a
misspelled filename in the #include statement(s)? You haven't shown that
#include anywhere, or told us the name(s) of the file(s) that those
#classes
are defined in, so we can't tell.

Here are both files complete, without anything cut out:

classes.h:


#ifndef CLASSES_H
#define CLASSES_H

#define ZAHL 10

class CBauteil {
private:
int m_iNumber;
int m_iWeight;
public:
int getNumber();
void setNumber(int);
void setWeight(int);
void printInfo();
};

class CDINBauteil : public CBauteil {
private:
int m_iDINNumber;
public:
int getDINNumber();
void setDINNumber(int);
void printDINInfo();
};

class CDatenbank {
private:
CBauteil m_parts[ZAHL];
int partCount;
CDINBauteil m_DINparts[ZAHL];
int DINpartCount;
public:
void writeDatabase();
void readDatabase();
void printDatabase();

void addPart(CBauteil);
void addDINPart(CDINBauteil);
void searchPart(int);
void searchDINPart(int);
};


#endif



classes.cpp:


#include "classes.h"
#include <iostream>
using namespace std;

int CBauteil::getNumber()
{
return m_iNumber;
}

void CBauteil::setNumber(int newNumber)
{
m_iNumber = newNumber;
}

void CBauteil::setWeight(int newWeight)
{
m_iWeight = newWeight;
}

void CBauteil::printInfo()
{
cout << "Bauteil Nr." << m_iNumber << "vom Gewicht" << m_iWeight
<< "kg." << endl;
}


int CDINBauteil::getDINNumber()
{
return m_iDINNumber;
}

void CDINBauteil::setDINNumber(int newDIN)
{
m_iDINNumber = newDIN;
}

void CDINBauteil::printDINInfo()
{
cout << "Norm: DIN " << m_iDINNumber << endl;
}




and my compile command:

florian@horus ~/Dokumente/Studium/SE/SE9 $ g++ classes.cpp
classes.cpp: In member function `int CDINBauteil::getDINNumber()':
classes.cpp:28: error: `m_iDINNumber' was not declared in this scope
classes.cpp: In member function `void CDINBauteil::setDINNumber(int)':
classes.cpp:33: error: `m_iDINNumber' was not declared in this scope
classes.cpp: In member function `void CDINBauteil::printDINInfo()':
classes.cpp:38: error: `m_iDINNumber' was not declared in this scope


Thanks,

Florian

Volker Lukas
Guest
 
Posts: n/a
#4: Aug 21 '06

re: Problem with scope


Florian Lindner wrote:
Quote:
[...]
Here are both files complete, without anything cut out:
I for one can not reproduce your problem. I tried 3 different versions of
GCC. If you extract the code from your posting and save it in a different,
complete new directory, and try it from scratch there, does it still fail
to compile?
Victor Bazarov
Guest
 
Posts: n/a
#5: Aug 21 '06

re: Problem with scope


Florian Lindner wrote:
Quote:
[..]
Here are both files complete, without anything cut out:
>
classes.h:
Bad name.
Quote:
[...]
>
classes.cpp:
Bad name, again.
Quote:
>
#include "classes.h"
Are you sure there isn't another "classes.h" file lying around and
getting picked up by the compiler?
Quote:
[...]
>
and my compile command:
>
florian@horus ~/Dokumente/Studium/SE/SE9 $ g++ classes.cpp
classes.cpp: In member function `int CDINBauteil::getDINNumber()':
classes.cpp:28: error: `m_iDINNumber' was not declared in this scope
classes.cpp: In member function `void CDINBauteil::setDINNumber(int)':
classes.cpp:33: error: `m_iDINNumber' was not declared in this scope
classes.cpp: In member function `void CDINBauteil::printDINInfo()':
classes.cpp:38: error: `m_iDINNumber' was not declared in this scope
>
It is customary to indicate in the posted source code which lines are
the ones your compiler is complaining about.

I tried your code with another compiler, it compiled fine. So, either
you posted code that you didn't compile (and didn't know about it), or
your compiler finds another "classes.h" file and not the one you give it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Howard
Guest
 
Posts: n/a
#6: Aug 21 '06

re: Problem with scope



"Florian Lindner" <Florian.Lindner@xgm.dewrote in message
news:eccm2k$g1k$1@news.in.tum.de...
Quote:
Howard wrote:
>
Quote:
>Or, perhaps you're not including the header file(s) correctly? Perhaps a
>misspelled filename in the #include statement(s)? You haven't shown that
>#include anywhere, or told us the name(s) of the file(s) that those
>#classes
>are defined in, so we can't tell.
>
>
Here are both files complete, without anything cut out:
>
and my compile command:
>
florian@horus ~/Dokumente/Studium/SE/SE9 $ g++ classes.cpp
classes.cpp: In member function `int CDINBauteil::getDINNumber()':
classes.cpp:28: error: `m_iDINNumber' was not declared in this scope
classes.cpp: In member function `void CDINBauteil::setDINNumber(int)':
classes.cpp:33: error: `m_iDINNumber' was not declared in this scope
classes.cpp: In member function `void CDINBauteil::printDINInfo()':
classes.cpp:38: error: `m_iDINNumber' was not declared in this scope
>
The code looks ok to me. Is that the entire list of warnings/errors?

I don't know much about command-line compiling, and I only use gcc via the
XCode IDE, but it may have something to do with the way you're compiling
this, or your environment setup. In any case, the code is fine. At least
for me, in VC++, it compiles fine.

I don't know what that path above specifies, but maybe there's a problem
finding that header. If so, though, I'd think it would complain, unless
it's finding a _different_ classes.h file. Hmmm, perhaps it's simply the
name "classes.h"? Is there a system file with that name? Or another file
named classes.h somewhere that it's finding? (You could try introducing an
obvious syntax error in your classes.h file to see if it's actually
including the one you want.)

If it's a gnu-related problem, then a gnu newsgroup might be the better
place to ask.

-Howard





Florian Lindner
Guest
 
Posts: n/a
#7: Aug 21 '06

re: Problem with scope


Howard wrote:
Quote:
>
"Florian Lindner" <Florian.Lindner@xgm.dewrote in message
news:eccm2k$g1k$1@news.in.tum.de...
Quote:
>Howard wrote:
>>
Quote:
>>Or, perhaps you're not including the header file(s) correctly? Perhaps
>>a
>>misspelled filename in the #include statement(s)? You haven't shown
>>that
>>#include anywhere, or told us the name(s) of the file(s) that those
>>#classes
>>are defined in, so we can't tell.
>>
>>
>Here are both files complete, without anything cut out:
>>
>and my compile command:
>>
>florian@horus ~/Dokumente/Studium/SE/SE9 $ g++ classes.cpp
>classes.cpp: In member function `int CDINBauteil::getDINNumber()':
>classes.cpp:28: error: `m_iDINNumber' was not declared in this scope
>classes.cpp: In member function `void CDINBauteil::setDINNumber(int)':
>classes.cpp:33: error: `m_iDINNumber' was not declared in this scope
>classes.cpp: In member function `void CDINBauteil::printDINInfo()':
>classes.cpp:38: error: `m_iDINNumber' was not declared in this scope
>>
>
The code looks ok to me. Is that the entire list of warnings/errors?
>
I don't know much about command-line compiling, and I only use gcc via the
XCode IDE, but it may have something to do with the way you're compiling
this, or your environment setup. In any case, the code is fine. At least
for me, in VC++, it compiles fine.
>
I don't know what that path above specifies, but maybe there's a problem
finding that header. If so, though, I'd think it would complain, unless
it's finding a _different_ classes.h file. Hmmm, perhaps it's simply the
name "classes.h"? Is there a system file with that name? Or another file
named classes.h somewhere that it's finding?
It really seemed to be a name clash. I've renamed it and it works fine now!
(the linker complains about missing main() but that's not a problem...)

Thanks,

Florian
Closed Thread