Thread: keeping the class alive

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    keeping the class alive

    hi,

    How can I keep my class and all datastructures alive in the following example:

    Code:
    class A{
    
      protected:
       
        vector<int> x;
     
      public:
    
      A();
     
      ~A();
    };
    
    A::A(){
      x.resize(10);
      for (int i =1; i< 10; i++)
        x[i] = i;
    }
    A::z(){
      cout << x[1] << endl;
    }
    
    class B : protected A {
    
      public:
    
      B();
    
      int f();
    
    };
    
    B::B(){
    
      A obj;
    }
    
    B::f(){
      this->z();
    }
    
    int main(){
      B o();
      
      o.f();  // it should print 1 but vector is of size 0 since, I guess it want out of scope;
    
    }
    so the idea is to construct class B from main which constructs class A. And after the construction I whish to call function f which calles function z of A but z cannot execute since vector x is empty. Why is it empty and how to keap it full untill B is destroyed?

    thnx

    PS
    I tried to illustrate the concept thus code might not be functionall

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This declares a function named o that takes no arguments and returns a B object:
    Code:
    B o();
    You probably wanted to write:
    Code:
    B o;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    I must be doing something seriously wrong. I was in a hurry before but now here is the actuall example

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    
    template<typename T>
    class AA{
    protected:
    
       vector<T> vx;
    public:
    
      AA(vector<T>& v);
    
    };
    
    template<typename T>
    AA<T>::AA(vector<T>& vec){
      vx.reserve(vec.size());
      copy(vec.begin(),vec.end(),back_inserter(vx));
    }
    
    
    // my derived class
    template<typename T>
    class B : public AA {
    
      public:
    
      B(vector<T>& v);
    
      void print_r(T x);
    };
    
    template<typename T>
    B<T>::B(vector<T>& vec){
    
      AA<T> a(vec);
    }
    
    template<typename T>
    void B<T>::print_r(T x){
      cout << this->vx[x] << endl;
    }
    
    int main (){
      vector<int> V(10,6);
      int k = 4;
      B<int> b(V);
      b.print_r(k);
    }
    my error states:

    Code:
    $ g++ -g  -std=c++0x -o class_1 class_1.cpp
    class_1.cpp:28:21: error: expected class-name before ‘{’ token
    class_1.cpp: In member function ‘void B<T>::print_r(T) [with T = int]’:
    class_1.cpp:51:14:   instantiated from here
    class_1.cpp:44:3: error: ‘class B<int>’ has no member named ‘vx’
    How to make this work ?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    AA is a template class, so you must specify the type, e.g.

    template<typename T>
    class B: public AA<T>
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would write your AA class template like this:
    Code:
    template<typename T>
    class AA
    {
    public:
        AA(const std::vector<T>& v) : vx(v) {}
    protected:
        std::vector<T> vx;
    };
    The reserve and copy with back_inserter is not necessary when copy construction will do. Furthermore, the parameter should be a const reference, not a non-const reference.

    Next, I would write your B class template like this:
    Code:
    template<typename T>
    class B : public AA<T>
    {
    public:
        B(const std::vector<T>& v) : AA<T>(v) {}
    
        void print_r(T x) const;
    };
    AA is a class template, hence you should write AA<T> in this context. Furthermore, in your code you appear to have uselessly created a local variable named a in B's constructor. My example initialises the AA<T> base class subobject instead.

    Notice that I declared print_r to be const since it does not modify the logical/observable state of the object. You should make the corresponding change in its definition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    ok lets correct this:

    Code:
    #include <iostream>
    #include <vector>
    #include <string>
     
    using namespace std;
     
     
    template<typename T>
    class AA{
    protected:
     
       vector<T> vx;
    public:
     
      AA(vector<T>& v);
     
    };
     
    template<typename T>
    AA<T>::AA(vector<T>& vec){
      vx.reserve(vec.size());
      copy(vec.begin(),vec.end(),back_inserter(vx));
    }
     
     
    // my derived class
    template<typename T>
    class B : public AA<T> {  // corrected
     
      public:
     
      B(vector<T>& v);
     
      void print_r(T x);
    };
     
    template<typename T>
    B<T>::B(vector<T>& vec){
     
      AA<T> a(vec);
    }
     
    template<typename T>
    void B<T>::print_r(T x){
      cout << this->vx[x] << endl;
    }
     
    int main (){
      vector<int> V(10,6);
      int k = 4;
      B<int> b(V);
      b.print_r(k);
    }

    now i get an error :


    Code:
    t$ g++ -g  -std=c++0x -o class_1 class_1.cpp
    class_1.cpp: In constructor ‘B<T>::B(std::vector<T>&) [with T = int]’:
    class_1.cpp:51:13:   instantiated from here
    class_1.cpp:38:23: error: no matching function for call to ‘AA<int>::AA()’
    class_1.cpp:38:23: note: candidates are:
    class_1.cpp:20:1: note: AA<T>::AA(std::vector<T>&) [with T = int]
    class_1.cpp:20:1: note:   candidate expects 1 argument, 0 provided
    class_1.cpp:9:7: note: AA<int>::AA(const AA<int>&)
    class_1.cpp:9:7: note:   candidate expects 1 argument, 0 provided
    class_1.cpp:9:7: note: AA<int>::AA(AA<int>&&)
    class_1.cpp:9:7: note:   candidate expects 1 argument, 0 provided

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    that is it , thank you laserlight now I have all the pieces for my study case. I'll bug you more if i don't connect the dots thank you once again !

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by baxy
    no matching function for call to ‘AA<int>::AA()’
    This is because AA does not have a default constructor. Since you did not explicitly initialise the base class subobject, its default constructor was invoked, but since there is no default constructor, you get this error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The reason why C is still alive
    By gerpzezeen in forum General Discussions
    Replies: 104
    Last Post: 05-25-2014, 03:27 PM
  2. Check if a host is alive
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 05-05-2009, 11:10 AM
  3. Keeping connection alive
    By VeX92 in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2007, 10:10 AM
  4. How do I check if the connection is still alive?
    By jaro in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 02:10 AM
  5. [C] Keep a process alive
    By BianConiglio in forum Windows Programming
    Replies: 6
    Last Post: 03-10-2005, 10:26 PM