Unresolved external on vector inside class constructor

This is a discussion on Unresolved external on vector inside class constructor within the C++ Programming forums, part of the General Programming Boards category; Having problems vector again. I'm studying static data members and doing a toy class to further my knowledge. This class ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,488

    Unresolved external on vector inside class constructor

    Having problems vector again. I'm studying static data members and doing a toy class to further my knowledge. This class has a single data member and several static members. I placed a few comments to better illustrate the class. The idea... to have a class that records the sum and average of its objects.

    Code:
    class counter {
    public:
        counter(int);
        
        inline int value() const;  //number getter
    
        //static members getters
        inline static unsigned long count();
        inline static unsigned long sum();
        inline static double avg();
        static double ravg(unsigned long x); //pass vector index, return avg.
    private:
        int number;
    
        static unsigned long count_; //object count
        static unsigned long sum_;  //sum of counter::number
        static double avg_;              //average of counter::number
        static std::vector<double> ravg_;  //holds running average
    };
    
    /* inline definitions are here */
    The constructor is being defined on the class cpp file. Also shown, the static members initialization.

    Code:
    unsigned long counter::count_ = 0;
    unsigned long counter::sum_ = 0;
    double counter::avg_ = 0;
    std::vector<double> ravg_(1, 0);
    
    counter::counter(int x = 0): number(x) {
        count_ += 1;
        sum_ += x;
        avg_ = static_cast<double>(sum_) / static_cast<double>(count_);
        ravg_.push_back(avg_); //unresolved external error
    }
    Why am I getting an unresolved external in the constructor pointing to the ravg_.push_back() line?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Quote Originally Posted by Mario F.
    Having problems vector again. I'm studying static data members and doing a toy class to further my knowledge. This class has a single data member and several static members. I placed a few comments to better illustrate the class. The idea... to have a class that records the sum and average of its objects.

    Code:
    class counter {
    public:
        counter(int);
        
        inline int value() const;  //number getter
    
        //static members getters
        inline static unsigned long count();
        inline static unsigned long sum();
        inline static double avg();
        static double ravg(unsigned long x); //pass vector index, return avg.
    private:
        int number;
    
        static unsigned long count_; //object count
        static unsigned long sum_;  //sum of counter::number
        static double avg_;              //average of counter::number
        static std::vector<double> ravg_;  //holds running average
    };
    
    /* inline definitions are here */
    The constructor is being defined on the class cpp file. Also shown, the static members initialization.

    Code:
    unsigned long counter::count_ = 0;
    unsigned long counter::sum_ = 0;
    double counter::avg_ = 0;
    std::vector<double> counter::ravg_(1, 0);
    
    counter::counter(int x = 0): number(x) {
        count_ += 1;
        sum_ += x;
        avg_ = static_cast<double>(sum_) / static_cast<double>(count_);
        ravg_.push_back(avg_); //unresolved external error
    }
    Why am I getting an unresolved external in the constructor pointing to the ravg_.push_back() line?
    See code in red above!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Gah, hk_mp5kpdw beat me to it
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,488
    *headdesks*

    The funny thing about all this is seeing me preaching about namespaces on another thread and forgetting about them in my own code. Tsk!

    Thanks both
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    FWIW, Typically if I had a container of classes containing values that I needed to calculate a sum and average for I'd make a seperate class to hold those statistics and call the for_each STL function on the container of objects instead of making some static variables.

    A brief example... something along the lines of:
    Code:
    class data
    {
        int value;
    public:
        data( int val = 0 ) : value(val) {}
        int GetValue() const
        {
            return value;
        }
    };
    
    class average
    {
        int sum;
        int count;
    public:
        average() : sum(0), count(0) {}
        void operator()(const data& item)
        {
            ++count;  // Increase number of objects processed
            sum += item.GetValue();
        }
        double GetAverage() const
        {
            return static_cast<double>(sum) / count;
        }
    };
    
    ...
    
    vector<data> data_vect;
    
    // Fill data_vect with some data objects
    data_vect.push_back( data(6) );
    data_vect.push_back( data(14) );
    
    // Calculate average of all the objects in data_vect
    average avg = for_each(data_vect.begin(), data_vect.end(), average());
    cout << "Average of all objects is: " << avg.GetAverage() << endl;
    Output should be:
    Code:
    Average of all objects is: 10
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Typically if I had a container of classes containing values that I needed to calculate a sum and average for I'd make a seperate class to hold those statistics and call the for_each STL function on the container of objects instead of making some static variables.
    I agree, though wouldnt you prefer std::accumulate to std::for_each for the sum, then use the sum to calculate the average?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,488
    Thanks hk_mp5kpdw.

    I understood the logic behind your implementation, so I'm sure i'll do just that if I ever need this type of functionality on my code.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    > std::accumulate
    I'm getting sick of running into stuff I don't have. My copy of Dec-C++ does not implement this algorithm. Guess I need to download the STL and install it or something.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    I'm getting sick of running into stuff I don't have. My copy of Dec-C++ does not implement this algorithm. Guess I need to download the STL and install it or something.
    It's in <numeric>
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Why. SGI said it was somewhere else.

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    Quote Originally Posted by citizen
    Why. SGI said it was somewhere else.
    They lied.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Why. SGI said it was somewhere else.
    The SGI STL reference notes that std::accumulate is:
    "Defined in the standard header numeric, and in the nonstandard backward-compatibility header algo.h."

    Incidentally, if you take the std::accumulate route, it might be something like:
    Code:
    class SumDataValue
    {
    public:
        int operator()(int init, const data& item)
        {
    	    return init + item.GetValue();
        }
    };
    
    // ...
    
    // Calculate average of all the objects in data_vect
    int sum = std::accumulate(data_vect.begin(), data_vect.end(), 0, SumDataValue());
    double avg = static_cast<double>(sum) / data_vect.size();
    cout << "Average of all objects is: " << avg << endl;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,488
    Ouch! Just go hold on some info on your implementation and it surely is painful .
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    I'm guessing that refers to the original STL. Almost every compiler in use now has the C++ standard library available, including SGI's implementations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. [GLUT] Pointers to class methods
    By cboard_member in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 03:03 PM
  3. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 01:36 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21