473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Problem with scope

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
Aug 21 '06 #1
6 1642

"Florian Lindner" <Fl*************@xgm.dewrote in message
news:ec**********@news.in.tum.de...
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?
>

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


Aug 21 '06 #2
Howard wrote:
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

Aug 21 '06 #3
Florian Lindner wrote:
[...]
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?
Aug 21 '06 #4
Florian Lindner wrote:
[..]
Here are both files complete, without anything cut out:

classes.h:
Bad name.
[...]

classes.cpp:
Bad name, again.
>
#include "classes.h"
Are you sure there isn't another "classes.h" file lying around and
getting picked up by the compiler?
[...]

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
Aug 21 '06 #5

"Florian Lindner" <Fl*************@xgm.dewrote in message
news:ec**********@news.in.tum.de...
Howard wrote:
>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

Aug 21 '06 #6
Howard wrote:
>
"Florian Lindner" <Fl*************@xgm.dewrote in message
news:ec**********@news.in.tum.de...
>Howard wrote:
>>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
Aug 21 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: f | last post by:
I am writing a java code generation tool. I use xml and xslt. But I have some problem using xsl:for-each. here is my xml <?xml version = "1.0"?> <CLASS package_name=".test"...
4
by: Jason80 | last post by:
First I wrote some _VBScript to get info from OS, and now I wrote some code in VB.Net, and I have a problem now. Look at this script in vbs List1.vbs: strComputer = "."
11
by: milkyway | last post by:
Hello, I have an HTML page that I am trying to import 2 .js file (I created) into. These files are: row_functions.js and data_check_functions.js. Whenever I bring the contents of the files into...
1
by: Nikolay Petrov | last post by:
I have an function which should enumerate shares on remote pc. The problem is the it always shows shares on my local pc. Code: Public Function GetNetworkShares(byval ComputerName as string) As...
6
by: easy | last post by:
The following code gives me compiler errors galore telling me that I'm crossing the initalization of the vectors and that case label 2 and default are "within scope of cleanup or variable array". ...
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
2
by: Kamen | last post by:
Hello there, I try to define the properties font-size and font-bold of the pager of a gridview using the pagerstyle, but the settings are not appleyd at run time. What could be the reason for...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
2
by: Vadim Malishev | last post by:
Hello, Can anybody help to solve the following problem? My Windows Service trying to access remote machine to get WindowsDirectory Property over Win32_OperatingSystem WMI class. Both servers...
5
by: smittie31 | last post by:
I am having a problem with a border around me html page. The border does not flow thru the whole html page, it cuts off halfway. --> See http://keithborom.com/marlon-sanders CSS STYLESHEET ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.