Thread: Unresolved external on vector inside class constructor

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    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,817
    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
    28,413
    Gah, hk_mp5kpdw beat me to it
    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

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    *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,817
    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
    28,413
    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?
    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

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > 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
    28,413
    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>
    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

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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,817
    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
    28,413
    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;
    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

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    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,366
    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, 04:03 PM
  3. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02: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