Hi all:
In my code I define a class with inline constructor. But it does not
work. I describe the class as below:
myclass{
public:
myclass(int a, int b) { r1 = a; r2 = b;}
protected:
int r1;
int r2;
double z;
}
But the constructor does not work. When I declare an object of the
class, the constructor can not correctly initialize the variables.
Then I modify the code as below:
myclass{
public:
myclass(int a, int b);
protected:
int r1;
int r2;
double z;
}
myclass::myclass(int a, int b) {
r1 = a;
r2 = b;
}
It works. When I declare an object of the class, the constructor can
correctly initialize variables.
What is the problem?
Thanks a lot.
John 13 1395
"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om... In my code I define a class with inline constructor. But it does not work. I describe the class as below:
myclass{ public: myclass(int a, int b) { r1 = a; r2 = b;}
protected: int r1; int r2; double z; }
But the constructor does not work. When I declare an object of the class, the constructor can not correctly initialize the variables.
How do you know the program isn't working? The code you show above is
obviously not the actual code, because it won't compile. So what did you
actually do, and how do you know that it's not behaving correctly?
"Andrew Koenig" <ar*@acm.org> wrote... "John" <jo*********@yahoo.com> wrote in message news:c3**************************@posting.google.c om...
In my code I define a class with inline constructor. But it does not work. I describe the class as below:
myclass{ public: myclass(int a, int b) { r1 = a; r2 = b;}
protected: int r1; int r2; double z; }
But the constructor does not work. When I declare an object of the class, the constructor can not correctly initialize the variables.
How do you know the program isn't working? The code you show above is obviously not the actual code, because it won't compile. So what did you actually do, and how do you know that it's not behaving correctly?
Absolutely agree with Andrew here. Just a side note, I've seen many
times that a newbie would write
myclass(int a, int b) { int r1 = a; int r2 = b; }
without even realising that it's different from the intended way. It
can even happen that while typing in their problem into a newsgroup
posting they inadvertently correct the error. Of course, it would be
nice if compilers warned about situations like that...
To the OP: do NOT use assignment, use the initialiser list.
Victor
"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om... Hi all:
In my code I define a class with inline constructor. But it does not work. I describe the class as below:
myclass{ public: myclass(int a, int b) { r1 = a; r2 = b;}
protected: int r1; int r2; double z; }
But the constructor does not work. When I declare an object of the class, the constructor can not correctly initialize the variables.
Then I modify the code as below:
myclass{ public: myclass(int a, int b);
protected: int r1; int r2; double z; }
myclass::myclass(int a, int b) { r1 = a; r2 = b; }
It works. When I declare an object of the class, the constructor can correctly initialize variables.
What is the problem?
Without seeing an entire program it is impossible to say. Both code examples
you posted are equally incorrect (missing semi colons, missing keywords etc)
but as far as writing an inline constructor goes you did that correctly. So
whatever your problem was it was somewhere in the code you didn't post. This
is a very common situation with newbies posting to this group, they don't
understand where they are going wrong and often post the part of the program
that is correct, not the part that is wrong. If you want help then post the
real code, and post a complete program.
john
Thanks for reply.
The code is not the original code. I wish I could post the original
code here. But I can not, because the code is large and complex. The
class in my post shows the structure of the class that generates
problem. In my original code, I output the result after an object is
declared, like:
myclass v(2,3);
std::cout<<v.a<<" "<<v.b<<std::endl;
The result is not 2 and 3, but some large numbers.
But when I do not use inline constructor, the problem does not happen.
I know it is difficult to figure out the problem without the original
code post here.
Thanks anyway.
John
"Andrew Koenig" <ar*@acm.org> wrote in message news:<sE*********************@bgtnsc05-news.ops.worldnet.att.net>... "John" <jo*********@yahoo.com> wrote in message news:c3**************************@posting.google.c om...
In my code I define a class with inline constructor. But it does not work. I describe the class as below:
myclass{ public: myclass(int a, int b) { r1 = a; r2 = b;}
protected: int r1; int r2; double z; }
But the constructor does not work. When I declare an object of the class, the constructor can not correctly initialize the variables.
How do you know the program isn't working? The code you show above is obviously not the actual code, because it won't compile. So what did you actually do, and how do you know that it's not behaving correctly?
"John" <jo*********@yahoo.com> wrote in message
news:c3*************************@posting.google.co m Thanks for reply. The code is not the original code. I wish I could post the original code here. But I can not, because the code is large and complex. The class in my post shows the structure of the class that generates problem.
If the code does not generate the problem, then it does NOT show the
structure of the class that generates the problem. Produce a code sample
that actually shows the problem and I am sure we can quickly solve it.
In my original code, I output the result after an object is declared, like:
myclass v(2,3); std::cout<<v.a<<" "<<v.b<<std::endl; The result is not 2 and 3, but some large numbers.
a and b are not members of myclass as you specified it (the members are r1
and r2). Further, those members are protected, so you cannot access them
from, say, main().
The following works.
#include <iostream>
class myclass{
public:
myclass(int a, int b) { r1 = a; r2 = b;}
void print()
{
std::cout<< r1 <<" "<< r2 <<std::endl;
}
protected:
int r1;
int r2;
double z;
};
int main()
{
myclass v(2,3);
v.print();
return 0;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
/*
This is a long shot but are you by any chance using a flakey compiler?
i.e. not Suns Forte C++, GNU c++, VC++ or any of the other big names.
If the compiler isn't very well known or is in beta release there's a
SLIM SLIM SLIM possibility that they've got the inlining bit wrong or
something. Just for kicks post your compilers version of CC
-V(ersion). Methinks you're using a well dodgy compiler or have an
error somewhere else in your code/app.
In any case, give this code a lash. If it prints out anything other
than
entered main
myclass.r1: 2 myclass.r2: 3
then we've narrowed down your problem a bit. If it does print out the
above text as above, then you have to look elsewhere as the inlined
constructor is not an issue.
BTW somebody mentioned that you should use the initialiser list in
your ctor. I think that applies only for types other than built in
types no? i.e. there's no overhead in using assignment with integers
etc. No?
*/
#include <iostream>
class myclass
{
public:
// if you get the expected output with this code, recompile without
the
// inline keyword and see what you get.
inline myclass(int a, int b)
{
r1 = a;
r2 = b;
}
// protected: you won't be able to get your hands on these members
externally.
// so I'll leave them in the public interface
int r1;
int r2;
double z;
};
int main(int argc, char** argv)
{
std::cout << "entered main" << std::endl;
myclass mc(2,3);
std::cout << "myclass.r1: " << mc.r1 << " myclass.r2: " << mc.r2 <<
std::endl;
return 0;
}
grahamo wrote:
BTW somebody mentioned that you should use the initialiser list in your ctor. I think that applies only for types other than built in types no? i.e. there's no overhead in using assignment with integers etc. No?
There is no overhead. But even with builtin types it is easy to
show an example where initializer lists work, while assignment doesn't.
Hint: Think of const members. Think of references.
So getting into the habit of using initializer lists doesn't do any
harm, but is vital in some cases.
--
Karl Heinz Buchegger kb******@gascad.at
> Thanks for reply. The code is not the original code. I wish I could post the original code here. But I can not, because the code is large and complex.
Ok, try to minimize and simplify your code so, that it is "simple" and
still buggy. It seems to me, that you should give us more detailed
description of the problem?
For example, compile this:
#include <iostream>
class A
{
public:
A(int a, int b)
{
a_ = a;
b_ = b;
}
int a_;
int b_;
};
int main()
{
A a(1,2);
std::cout<<a.a_<<" "<<a.b_<<std::endl;
}
In this example we have 1 2 in output. And so your example does not
show your problem, right?
Victor Bazarov posted: To the OP: do NOT use assignment, use the initialiser list.
Just to expand on that a little...
Consider the following:
class BankAccountData
{
public:
unsigned long int const ID;
BankAccountData(unsigned long int& const in_ID) : ID(in_ID)
{
;
}
};
Without the initializer list, you couldn't work with ID, as it is const.
-JKop
Hi all:
Thanks a lot. I post a simplified version of my code.
---------------
header-file.h
---------------
#include <list>
#include <algorithm>
myclass{
friend class yclass;
public:
myclass(u_int32_t a, u_int32_t b) { r1 = a; r2 = b;}
protected: //u_int32_t is defined type, a kind of integer.
u_int32_t r1;
u_int32_t r2;
double z;
}
yclass{
public:
.....
void m_insert(u_int32_t id, u_int32_t bd);
bool m_lookup(u_int32_t id, u_int32_t bd);
void m_purge(void);
void funct(u_int32_t id, u_int32_t bd);
std::list< myclass > myclass_list;
.....
}
-----------------------------------
file.cc
-----------------------------------
#include <header-file.h>
void yclass::m_insert(u_int32_t d, u_int32_t bd) {
myclass b(d, bd);
std::cout<<"r1:"<<b.r1<<" r2:"<<b.r2<<std::endl; //LINE 1
b.z = TIME;//a constant.
myclass_list.push_back(b);
std::cout<<"insert--d:"<<d<<" bd:"<< bd<<std::endl;
std::list< myclass >::const_iterator pos;
for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos)
std::cout<<"pos->r1:"<<(*pos).r1<<" r2:"<<(*pos).r2<<"
z:"<<pos->z<<std::endl;
}
bool yclass::m_lookup(u_int32_t d, u_int32_t bd) {
std::list< myclass >::const_iterator pos;
std::cout<<"lookup--d:"<<d<<" bd:"<< bd<<std::endl;
for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){
std::cout<<"(*pos).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::endl;
if (((*pos).r1 == d) && ((*pos).r2 == bd)){
std::cout<<"find it"<<std::endl; //LINE 2
return true;
}
}
return false;
}
void yclass::m_purge() {
std::list< myclass >::iterator pos;
for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){
if((*pos).z <= 10) {
myclass_list.erase(pos);
--pos;
}
}
}
void funct(u_int32_t id, u_int32_t bd){
.........
if(!m_lookup(id, bd)){
m_insert(id, bd); //LINE 3
}
.........
}
If LINE 3 is executed, e.g., m_insert(2,3), LINE 1 can not output 2
and 3, but two large numbers. So LINE 2 is never executed, since the
list -- myclass_list does not store the correct value. But if I do not
use inline constructor, the output is correct.
I post all the code related to myclass. I hope the bug has been
exposed.
Thanks again for the help.
John tp*****@mail.ru (New_user) wrote in message news:<d2**************************@posting.google. com>... Thanks for reply. The code is not the original code. I wish I could post the original code here. But I can not, because the code is large and complex.
Ok, try to minimize and simplify your code so, that it is "simple" and still buggy. It seems to me, that you should give us more detailed description of the problem?
For example, compile this:
#include <iostream>
class A { public: A(int a, int b) { a_ = a; b_ = b; } int a_; int b_; };
int main() { A a(1,2); std::cout<<a.a_<<" "<<a.b_<<std::endl; }
In this example we have 1 2 in output. And so your example does not show your problem, right?
Forgive the top post, but again the code you provided below is not
compilable, even with cursory examination. ie: myclass{ ... } is not C++,
You need to prefix with the keyword 'class' and terminate the class
declaraton with ';'
Get this code to compile and show us the problem.
Jeff F
"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om... Hi all:
Thanks a lot. I post a simplified version of my code.
--------------- header-file.h --------------- #include <list> #include <algorithm>
myclass{ friend class yclass; public: myclass(u_int32_t a, u_int32_t b) { r1 = a; r2 = b;} protected: //u_int32_t is defined type, a kind of integer. u_int32_t r1; u_int32_t r2; double z; }
yclass{
public:
..... void m_insert(u_int32_t id, u_int32_t bd); bool m_lookup(u_int32_t id, u_int32_t bd); void m_purge(void); void funct(u_int32_t id, u_int32_t bd);
std::list< myclass > myclass_list;
..... }
----------------------------------- file.cc ----------------------------------- #include <header-file.h>
void yclass::m_insert(u_int32_t d, u_int32_t bd) { myclass b(d, bd); std::cout<<"r1:"<<b.r1<<" r2:"<<b.r2<<std::endl; //LINE 1 b.z = TIME;//a constant. myclass_list.push_back(b);
std::cout<<"insert--d:"<<d<<" bd:"<< bd<<std::endl; std::list< myclass >::const_iterator pos; for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos) std::cout<<"pos->r1:"<<(*pos).r1<<" r2:"<<(*pos).r2<<" z:"<<pos->z<<std::endl;
}
bool yclass::m_lookup(u_int32_t d, u_int32_t bd) { std::list< myclass >::const_iterator pos; std::cout<<"lookup--d:"<<d<<" bd:"<< bd<<std::endl; for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){ std::cout<<"(*pos).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::endl; if (((*pos).r1 == d) && ((*pos).r2 == bd)){ std::cout<<"find it"<<std::endl; //LINE 2 return true; } } return false; }
void yclass::m_purge() { std::list< myclass >::iterator pos; for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){ if((*pos).z <= 10) { myclass_list.erase(pos); --pos; } } }
void funct(u_int32_t id, u_int32_t bd){
......... if(!m_lookup(id, bd)){ m_insert(id, bd); //LINE 3 }
.........
}
If LINE 3 is executed, e.g., m_insert(2,3), LINE 1 can not output 2 and 3, but two large numbers. So LINE 2 is never executed, since the list -- myclass_list does not store the correct value. But if I do not use inline constructor, the output is correct.
I post all the code related to myclass. I hope the bug has been exposed.
Thanks again for the help.
John tp*****@mail.ru (New_user) wrote in message
news:<d2**************************@posting.google. com>... Thanks for reply. The code is not the original code. I wish I could post the original code here. But I can not, because the code is large and complex.
Ok, try to minimize and simplify your code so, that it is "simple" and still buggy. It seems to me, that you should give us more detailed description of the problem?
For example, compile this:
#include <iostream>
class A { public: A(int a, int b) { a_ = a; b_ = b; } int a_; int b_; };
int main() { A a(1,2); std::cout<<a.a_<<" "<<a.b_<<std::endl; }
In this example we have 1 2 in output. And so your example does not show your problem, right?
"John" <jo*********@yahoo.com> wrote... Thanks a lot. I post a simplified version of my code.
Too "simplified" to be useful. Please read the FAQ 5.8. --------------- header-file.h --------------- #include <list> #include <algorithm>
This doesn't seem to be relevant at all. myclass{ friend class yclass; public: myclass(u_int32_t a, u_int32_t b) { r1 = a; r2 = b;} protected: //u_int32_t is defined type, a kind of integer. u_int32_t r1; u_int32_t r2; double z; }
yclass{
public:
..... void m_insert(u_int32_t id, u_int32_t bd); bool m_lookup(u_int32_t id, u_int32_t bd); void m_purge(void); void funct(u_int32_t id, u_int32_t bd);
std::list< myclass > myclass_list;
..... }
----------------------------------- file.cc ----------------------------------- #include <header-file.h>
void yclass::m_insert(u_int32_t d, u_int32_t bd) {
What are the values of these arguments when you step into this
function?
myclass b(d, bd);
What do you see in the debugger when you step over this definition?
std::cout<<"r1:"<<b.r1<<" r2:"<<b.r2<<std::endl; //LINE 1 b.z = TIME;//a constant. myclass_list.push_back(b);
std::cout<<"insert--d:"<<d<<" bd:"<< bd<<std::endl; std::list< myclass >::const_iterator pos; for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos) std::cout<<"pos->r1:"<<(*pos).r1<<" r2:"<<(*pos).r2<<" z:"<<pos->z<<std::endl;
}
bool yclass::m_lookup(u_int32_t d, u_int32_t bd) {
What are the values of the arguments here? Are they correct?
std::list< myclass >::const_iterator pos; std::cout<<"lookup--d:"<<d<<" bd:"<< bd<<std::endl;
What output do you see here?
for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){ std::cout<<"(*pos).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::endl; if (((*pos).r1 == d) && ((*pos).r2 == bd)){ std::cout<<"find it"<<std::endl; //LINE 2 return true; } } return false; }
void yclass::m_purge() { std::list< myclass >::iterator pos; for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){ if((*pos).z <= 10) { myclass_list.erase(pos); --pos; } } }
Seems that 'm_purge' is also errelevant. void funct(u_int32_t id, u_int32_t bd)
{
What are the values of 'id' and 'bd' here? ......... if(!m_lookup(id, bd)){ m_insert(id, bd); //LINE 3 }
.........
}
Who is calling this function? How is it called? If LINE 3 is executed, e.g., m_insert(2,3), LINE 1 can not output 2 and 3, but two large numbers. So LINE 2 is never executed, since the list -- myclass_list does not store the correct value. But if I do not use inline constructor, the output is correct.
I post all the code related to myclass.
No, you didn't post _all_ code. You posted _some_ code. Non-compilable,
first. Incomplete, second.
I hope the bug has been exposed.
How can it have been? So far only your inattentiveness to requirements
has.
V
"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om... Hi all:
Thanks a lot. I post a simplified version of my code.
--------------- header-file.h --------------- #include <list> #include <algorithm>
myclass{ friend class yclass; public: myclass(u_int32_t a, u_int32_t b) { r1 = a; r2 = b;} protected: //u_int32_t is defined type, a kind of integer. u_int32_t r1; u_int32_t r2; double z; }
yclass{
public:
..... void m_insert(u_int32_t id, u_int32_t bd); bool m_lookup(u_int32_t id, u_int32_t bd); void m_purge(void); void funct(u_int32_t id, u_int32_t bd);
std::list< myclass > myclass_list;
..... }
----------------------------------- file.cc ----------------------------------- #include <header-file.h>
void yclass::m_insert(u_int32_t d, u_int32_t bd) { myclass b(d, bd); std::cout<<"r1:"<<b.r1<<" r2:"<<b.r2<<std::endl; //LINE 1 b.z = TIME;//a constant. myclass_list.push_back(b);
std::cout<<"insert--d:"<<d<<" bd:"<< bd<<std::endl; std::list< myclass >::const_iterator pos; for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos) std::cout<<"pos->r1:"<<(*pos).r1<<" r2:"<<(*pos).r2<<" z:"<<pos->z<<std::endl;
}
bool yclass::m_lookup(u_int32_t d, u_int32_t bd) { std::list< myclass >::const_iterator pos; std::cout<<"lookup--d:"<<d<<" bd:"<< bd<<std::endl; for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){ std::cout<<"(*pos).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::endl; if (((*pos).r1 == d) && ((*pos).r2 == bd)){ std::cout<<"find it"<<std::endl; //LINE 2 return true; } } return false; }
void yclass::m_purge() { std::list< myclass >::iterator pos; for (pos = myclass_list.begin(); pos != myclass_list.end(); ++pos){ if((*pos).z <= 10) { myclass_list.erase(pos); --pos; } } }
void funct(u_int32_t id, u_int32_t bd){
......... if(!m_lookup(id, bd)){ m_insert(id, bd); //LINE 3 }
.........
}
If LINE 3 is executed, e.g., m_insert(2,3), LINE 1 can not output 2 and 3, but two large numbers. So LINE 2 is never executed, since the list -- myclass_list does not store the correct value. But if I do not use inline constructor, the output is correct.
I post all the code related to myclass. I hope the bug has been exposed.
Perhaps the problem is related to u_int32_t, how is that defined?
There is nothing wrong with your inline constructor.
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: grundmann |
last post by:
Hello,
i got a strange compiler error.
When compiling the following:
// forward declarations
typedef AvlTree<LineSegment,LineSegmentComperator> LSTree;
void handleEventPoint (const...
|
by: alkee.na |
last post by:
Hey,
Here's a sample code you can easily guess the result. <result 1>
#include <iostream>
using namespace std;
class AA
{
public:
AA() { cout<<"AA();"<<endl; }
|
by: Charles Law |
last post by:
Further to my issue about user controls, I have a problem with DesignMode.
Here is the project hierarchy:
MainApp
|_ Project1
|_ SubProject (UserControl)
SubProject has a default constructor...
|
by: rdh |
last post by:
I'm using List<> to store a class of positions.
Position are basically latitude and longitudes (doubles).
the line is something like
List<position> P = new List<position>();
Then I start...
|
by: senfo |
last post by:
I developed a Windows control in VS 2005 that inherits from the
PictureBox Control that adds the ability to select images in a Windows
application. It is, however, experiencing a strange issue...
|
by: charlie |
last post by:
Hi,
I found an article on informit.com that talks about C++ casts
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=285&rl=1
The strange thing is the author says the following...
|
by: StephQ |
last post by:
I have a class Bounds with two constructors:
class Bounds
{
private:
list<SegmentupperLinearSpline; // Upper bound.
list<SegmentlowerLinearSpline; // Lower bound.
....
|
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= |
last post by:
Yesterday Visual Studio gave me a strange error both at compiletime and at
designtime that had no obvious connection to anything I had changed recently.
After some effort tracking down the problem...
|
by: Dormilich |
last post by:
Hi,
I’ve encountered quite a strange error when loading one of my scripts in Safari (4.0.3/Mac):
I have no idea, what Safari is complaining about, since it works in FF 3.5 and Opera 10, does...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |