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,164

    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?
    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,681
    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!
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    19,544
    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,164
    *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
    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,681
    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
    I used to be an adventurer like you... then I took an arrow to the knee.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    19,544
    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,164
    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.
    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
    6,897
    > 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
    19,544
    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
    6,897
    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,681
    Quote Originally Posted by citizen
    Why. SGI said it was somewhere else.
    They lied.
    I used to be an adventurer like you... then I took an arrow to the knee.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    19,544
    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,164
    Ouch! Just go hold on some info on your implementation and it surely is painful .
    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,319
    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 ahluka in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2006, 03:03 PM
  3. wxWidgets link problem
    By ahluka 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