473,830 Members | 2,062 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange problem about constructor

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::myclas s(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
Jul 22 '05
13 1436
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_int3 2_t id, u_int32_t bd);
bool m_lookup(u_int3 2_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_inser t(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.pu sh_back(b);

std::cout<<"ins ert--d:"<<d<<" bd:"<< bd<<std::endl;
std::list< myclass >::const_iterat or pos;
for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos)
std::cout<<"pos->r1:"<<(*pos).r 1<<" r2:"<<(*pos).r2 <<"
z:"<<pos->z<<std::endl ;

}

bool yclass::m_looku p(u_int32_t d, u_int32_t bd) {
std::list< myclass >::const_iterat or pos;
std::cout<<"loo kup--d:"<<d<<" bd:"<< bd<<std::endl;
for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
std::cout<<"(*p os).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::end l;
if (((*pos).r1 == d) && ((*pos).r2 == bd)){
std::cout<<"fin d it"<<std::end l; //LINE 2
return true;
}
}
return false;
}

void yclass::m_purge () {
std::list< myclass >::iterator pos;
for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
if((*pos).z <= 10) {
myclass_list.er ase(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.go ogle.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::e ndl;
}

In this example we have 1 2 in output. And so your example does not
show your problem, right?

Jul 22 '05 #11
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*********@ya hoo.com> wrote in message
news:c3******** *************** ***@posting.goo gle.com...
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_int3 2_t id, u_int32_t bd);
bool m_lookup(u_int3 2_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_inser t(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.pu sh_back(b);

std::cout<<"ins ert--d:"<<d<<" bd:"<< bd<<std::endl;
std::list< myclass >::const_iterat or pos;
for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos)
std::cout<<"pos->r1:"<<(*pos).r 1<<" r2:"<<(*pos).r2 <<"
z:"<<pos->z<<std::endl ;

}

bool yclass::m_looku p(u_int32_t d, u_int32_t bd) {
std::list< myclass >::const_iterat or pos;
std::cout<<"loo kup--d:"<<d<<" bd:"<< bd<<std::endl;
for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
std::cout<<"(*p os).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::end l;
if (((*pos).r1 == d) && ((*pos).r2 == bd)){
std::cout<<"fin d it"<<std::end l; //LINE 2
return true;
}
}
return false;
}

void yclass::m_purge () {
std::list< myclass >::iterator pos;
for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
if((*pos).z <= 10) {
myclass_list.er ase(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.go ogle.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::e ndl;
}

In this example we have 1 2 in output. And so your example does not
show your problem, right?

Jul 22 '05 #12
"John" <jo*********@ya hoo.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_int3 2_t id, u_int32_t bd);
bool m_lookup(u_int3 2_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_inser t(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.pu sh_back(b);

std::cout<<"ins ert--d:"<<d<<" bd:"<< bd<<std::endl;
std::list< myclass >::const_iterat or pos;
for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos)
std::cout<<"pos->r1:"<<(*pos).r 1<<" r2:"<<(*pos).r2 <<"
z:"<<pos->z<<std::endl ;

}

bool yclass::m_looku p(u_int32_t d, u_int32_t bd) {
What are the values of the arguments here? Are they correct?
std::list< myclass >::const_iterat or pos;
std::cout<<"loo kup--d:"<<d<<" bd:"<< bd<<std::endl;
What output do you see here?
for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
std::cout<<"(*p os).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::end l;
if (((*pos).r1 == d) && ((*pos).r2 == bd)){
std::cout<<"fin d it"<<std::end l; //LINE 2
return true;
}
}
return false;
}

void yclass::m_purge () {
std::list< myclass >::iterator pos;
for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
if((*pos).z <= 10) {
myclass_list.er ase(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
Jul 22 '05 #13

"John" <jo*********@ya hoo.com> wrote in message
news:c3******** *************** ***@posting.goo gle.com...
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_int3 2_t id, u_int32_t bd);
bool m_lookup(u_int3 2_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_inser t(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.pu sh_back(b);

std::cout<<"ins ert--d:"<<d<<" bd:"<< bd<<std::endl;
std::list< myclass >::const_iterat or pos;
for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos)
std::cout<<"pos->r1:"<<(*pos).r 1<<" r2:"<<(*pos).r2 <<"
z:"<<pos->z<<std::endl ;

}

bool yclass::m_looku p(u_int32_t d, u_int32_t bd) {
std::list< myclass >::const_iterat or pos;
std::cout<<"loo kup--d:"<<d<<" bd:"<< bd<<std::endl;
for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
std::cout<<"(*p os).r1:"<<pos->r1<<" r2:"<< pos->r2<<std::end l;
if (((*pos).r1 == d) && ((*pos).r2 == bd)){
std::cout<<"fin d it"<<std::end l; //LINE 2
return true;
}
}
return false;
}

void yclass::m_purge () {
std::list< myclass >::iterator pos;
for (pos = myclass_list.be gin(); pos != myclass_list.en d(); ++pos){
if((*pos).z <= 10) {
myclass_list.er ase(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
Jul 22 '05 #14

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

Similar topics

8
1832
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 EventPoint& , LSTree& , double&, std::list<IntersectionPoint>& );
4
1300
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; }
29
5083
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 (New) which does the MyBase.New() thing and then InitializeComponent().
3
1407
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 adding to the List. P.add(Positions);
3
2434
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 that I can't explain. One of the desired affects was to provide the ability to change the background color on images that were selected, as well as setting the background color back to its original color after the image had been deselected. To...
28
2151
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 code will *not* compile: double d=15.95; int n= static_cast<int(d);
12
2277
by: StephQ | last post by:
I have a class Bounds with two constructors: class Bounds { private: list<SegmentupperLinearSpline; // Upper bound. list<SegmentlowerLinearSpline; // Lower bound. ....
6
3346
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 I discovered first a workaround, then the real cause of the problem. I would like to understand why what I am doing is frowned upon by Visual Studio and how to do this properly. My application is in one solution; supporting libraries including...
12
5657
Dormilich
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 anyone of you have an idea? thanks, Dormi // by Gavin Kistner
0
9781
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9641
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10769
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10477
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10197
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5615
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3072
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.