Re: Strange problem about constructor

From: Jeff Flinn (NONONE_at_nowhere.com)
Date: 06/10/04


Date: Thu, 10 Jun 2004 15:06:37 -0400

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" <johnw822003@yahoo.com> wrote in message
news:c30e885a.0406101043.7f3c8d42@posting.google.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_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
>
>
> tpochep@mail.ru (New_user) wrote in message
news:<d29bee5d.0406090454.605ee6b8@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?



Relevant Pages

  • Implication of project dependencies
    ... I am working on a project with VC++ 2003 and it compile & link well, ... myclass & myclass::GetInstance ... responsible of that are the project dependencies. ... this manual reference and checked the project myclass in the project ...
    (microsoft.public.vsnet.ide)
  • Implication of project dependencies
    ... I am working on a project with VC++ 2003 and it compile & link well, ... myclass & myclass::GetInstance ... responsible of that are the project dependencies. ... this manual reference and checked the project myclass in the project ...
    (microsoft.public.vc.ide_general)
  • Re: Organising classes in C# in Visual Studio ?
    ... > Visual Studio copying the Solution to the folder that adds the MyClass? ... > don't want to compile it to a dll - I better like to have a link or ... > something to the source code file itself! ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: class template
    ... > I defined a class template and some member variables and ... > class MyClass ... compile and the exact errorthat you're getting from the compiler. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: This I simply cant swallow
    ... encapsulation and abstract data types are completely orthogonal ... int i; ... MyClass i; //An instance of MyClass ... An ADT is a type that cannot, itself, be instantiated. ...
    (alt.comp.lang.learn.c-cpp)