Thread: dynamic #define

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    dynamic #define

    I have .h file that had a #define on top and 3-5 classes there and everything worked like a charm.
    However, I realize that this what I had defined may need to change some times. So, could I use a global variable (bliax). I probably can, but I think I can use a static variable or something. What do you suggest?

    I thought getting it from the BIG class that contains all the others, but I want to define an array (not really a vector), like
    Code:
    #define N 5
    
    ...
    A array[N+1];
    However A has some pure virtual functions and I remember correctly, I can not create an instance of an abstract class. Only play around with pointers or references.
    I mean, if I try to do
    Code:
    class B : public A{
      B() { 
        array = new A[5];
         OR
        for(int i = 0 ; i < 5 ; ++i)
             vector = push_back((A()));
      }
    I will receive compilation errors, so what do you suggest?

    I also tried by removing the "=0", going from pure virtuals to just virtuals, but then I got
    Code:
    samaras@samaras-A15:~$ g++ -Wall px.cpp -o px
    /tmp/ccDCzdcC.o: In function `A::A()':
    px.cpp:(.text._ZN1AC2Ev[_ZN1AC5Ev]+0x8): undefined reference to `vtable for A'
    /tmp/ccDCzdcC.o: In function `A::A(A const&)':
    px.cpp:(.text._ZN1AC2ERKS_[_ZN1AC5ERKS_]+0x8): undefined reference to `vtable for A'
    /tmp/ccDCzdcC.o: In function `A::~A()':
    px.cpp:(.text._ZN1AD2Ev[_ZN1AD5Ev]+0x8): undefined reference to `vtable for A'
    /tmp/ccDCzdcC.o:(.rodata._ZTI1B[typeinfo for B]+0x8): undefined reference to `typeinfo for A'
    collect2: ld returned 1 exit status
    As you can see, it can not find it to the vtable

    EDIT: The title I gave can be a nominee for The Worst Title of cboard for 2013
    Last edited by std10093; 11-10-2013 at 05:01 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Where is the code for A ?

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    For which of the two parts? For the abstract class creation error? If yes, take this similar simple main.cpp
    Code:
    #include <iostream>
    #include <vector>
    
    class A {
    public:
            virtual void bla() = 0;
            friend std::ostream& operator << ( std::ostream& s, const A& p )  {
                    s << p.a;
                    return s;
            }
            int a;
    };
    
    class B : public A{
    public:
            B(int a, int b) {
                    array = new A[5];
            }
            virtual void bla() { std::cout << "I am bla\n" << std::endl; }
    
    private:
            A* array;
    };
    
    int main()
    {
            B b(2, 2);
            return 0;
    }
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    So, what is your question?
    You already know you can not do that.

    Do you want suggestions for alternative designs ?
    If so, you have to specify what you are designing.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by manasij7479 View Post
    So, what is your question?
    You already know you can not do that.

    Do you want suggestions for alternative designs ?
    If so, you have to specify what you are designing.
    I think that I have to use something like a global variable, like the #define I had. But, because I had in mind that the #define can reduce my options, I have used it only inside the .h file.
    The question is, what I should use, in order to replace the #define, but get in file (only) scope only?

    Moreover, I wanted to be sure that I remember correctly, I can not do that.

    EDIT: Almost forgot, I want to set that "global in file scoped" variable dynamically, in the constructor for example.
    Last edited by std10093; 11-10-2013 at 05:31 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Pass an std::vector<A*> from the constructor, maybe.
    That will be more usable and look cleaner.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First this is C++, you should prefer the const over #define.

    Second why do you even need a constant if you're going to use new?

    Third you should be using initialization lists instead of putting the initialization in the constructor body.

    Jim

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    1)Good point.

    2)I do not want. My initial purpose is to do in the private part of the class
    Code:
    #define N 5 // If I use here const, as you pointed, it will become a global variable, right?
    
    AbstractClassType array[N+1];
    That's what I have to do! But, I see that N has to be taken from input.

    3)You mean initiazling the array from the in. list? How?

    @India guy, this solution requires me to change a lot of code now and I think using a static (?) variable in the .h is going to be ok.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    3)You mean initiazling the array from the in. list? How?
    Code:
            B(int a, int b) : array(new A[5]) {
    By the way what is the purpose of a and b in your constructor?


    Also are you sure you want to have an array of A in your B class? If so why do you have the inheritance?

    That's what I have to do! But, I see that N has to be taken from input.
    What do you mean by "N has to be taken from input"?

    Jim

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You mean like this?
    Code:
    #include <iostream>
    #include <vector>
    
    class A {
    public:
            virtual void bla() = 0;
            friend std::ostream& operator << ( std::ostream& s, const A& p )  {
                    s << p.a;
                    return s;
            }
            int a;
    };
    
    class B : public A{
    public:
            B() : array(new A[5]) {}
            virtual void bla() { std::cout << "I am bla\n" << std::endl; }
    
    private:
            A *array;
    };
    
    int main()
    {
            B b();
            return 0;
    }
    Same error as my approach
    Code:
    samaras@samaras-A15:~$ g++ -Wall px.cpp -o px
    px.cpp: In constructor ‘B::B()’:
    px.cpp:16:28: error: cannot allocate an object of abstract type ‘A’
    px.cpp:4:7: note:   because the following virtual functions are pure within ‘A’:
    px.cpp:6:22: note: 	virtual void A::bla()
    a and b are left-overs from another example. I discarded them.

    >Also are you sure you want to have an array of A in your B class?
    >If so why do you have the inheritance?
    I guess so, since I need an array, which will be of type of a base (abstract) class. Then there are two classes that inherit from this base. I want to cast depending on what I have in the .cpp. I do not feel like changing that now.

    >What do you mean by "N has to be taken from input"?
    I mean that N is not a constant as I thought in the start. Now, N is provided by the input. by the user.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    px.cpp:16:28: error: cannot allocate an object of abstract type ‘A’
    That's why I was asking about the array. A is a abstract type so you can't create an instance of an abstract base class.

    Normally you use either inheritance or polymorphism but not both. Remember with the inheritance your B class now contains the methods and variables from the base class.

    >Also are you sure you want to have an array of A in your B class?
    >If so why do you have the inheritance?
    I guess so, since I need an array, which will be of type of a base (abstract) class. Then there are two classes that inherit from this base. I want to cast depending on what I have in the .cpp. I do not feel like changing that now.
    But the array probably shouldn't be part of this class, it should be either part of another class or created outside the class, in main() for example.

    Jim

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Ok guys, thanks for the tips.

    But, I want to test if my logic on the problem I am working on works. So, as a temporary solution, instead of modifying the #define by hand, can I do something else? Like having a global/static variable or something and initializing it in the constructor of one class (the BIG one) that is contained in this file?
    Last edited by std10093; 11-10-2013 at 06:22 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    But, I want to test if my logic on the problem I am working on works. So, as a temporary solution, instead of modifying the #define by hand, can I do something else?
    No. If it doesn't work with the #define, which is a global constant value, it won't work by putting something strange in your class.

    The problem is the design of your classes. Like I said trying to use both polymorphism and inheritance of the same class is the wrong approach.


    Jim

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Hmm, maybe you are right, but I do not know how to change it. The code is big, so I do not think that it would be appropriate to post it here. Anyway... I will think.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by std10093 View Post
    The code is big, so I do not think that it would be appropriate to post it here. Anyway... I will think.
    Then try to make a small, minimal example without the nitty, gritty details?
    I also react on the fact that you want some array of size N + k of some pointers to an abstract class, but you later specify that N will change depending on input. Then what is the point of having N as a variable? You should probably pass size to these functions/classes that needs them directly and possibly notify them if the size of their internal array ("N") needs to change.
    I also get somewhat suspicious when you say you want to cast pointers. Why?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 12-03-2011, 02:26 PM
  2. #define
    By Ducky in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2009, 08:20 AM
  3. difference between #define and #define ()
    By bored_guy in forum C Programming
    Replies: 8
    Last Post: 07-20-2009, 06:58 PM
  4. How do I define this?
    By noob programmer in forum C Programming
    Replies: 3
    Last Post: 10-29-2008, 02:22 AM
  5. using define
    By ygfperson in forum C Programming
    Replies: 1
    Last Post: 04-25-2002, 09:58 PM