"undefined reference" in my Class

This is a discussion on "undefined reference" in my Class within the C++ Programming forums, part of the General Programming Boards category; Hi. I'm new to this whole stuff, and I'm trying to make my own class to solve a range of ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    9

    "undefined reference" in my Class

    Hi. I'm new to this whole stuff, and I'm trying to make my own class to solve a range of maths problems. The class, unruprisingly, is called numbers.

    There are 3 files which are causing me problems: numbers.h, problem27.cpp, numbers.cpp. I'll include the sources below. My problem is the error message, when I try to execute problem27.cpp.
    /home/bumcheekcity/Desktop/C++/euler/problem27/src/problem27.cpp:13: undefined reference to `numbers::numbers()'
    Line 13, btw, is the line numbers num; in problem27.cpp. What am I doing wrong? I'm using "Problem Solving, Abstraction and Design in C++" by Friedman to help me learn, and programming on Ubuntu Linux, using KDevelop. Any assistance will be much appreciated.

    numbers.h
    Code:
    //These lines are used to make sure that there are no problems with multiple definitions.
    #ifndef NUMBERS_H_
    #define NUMBERS_H_
    
    class numbers
    {
    	public :
    		//Constructors
    		numbers();
    		numbers(float);
    
    		//Obvious ones
    		void decrement();
    		void increment();
    		bool is_int();
    		bool is_prime();
    		void set_value(float);
    		float show_value();
    	
    
    	private:
    		float value;
    };
    
    #endif
    problem27.cpp
    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    #include <iostream>
    #include <cstdlib>
    #include "numbers.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	numbers num;
    	cout << "is the value";
    	//num.increment();
    	cout << "is the new value";
    	return EXIT_SUCCESS;
    }
    numbers.cpp
    Code:
    #include <iostream>
    #include <maths>
    #include "numbers.h"
    
    //Here are the two constructors, one with, one without argument.
    numbers::numbers()
    {
    	value = 0;
    }
    numbers::numbers(float initial_value)
    {
    	value = initial_value;
    }
    
    //End constructors.
    void decrement()
    {
    	value--;
    }
    void increment()
    {
    	value++;
    }
    bool is_int()
    {
    
    }
    
    bool is_prime()
    {
    	//Obvious
    }
    
    void set_value(float new_value)
    {
    	value = new_value;
    }
    
    float show_value()
    {
    	return value;
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    add numbers.cpp to the project.
    or compile like this
    Code:
    g++ problem27.cpp numbers.cpp -o problem27
    Kurt

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > void decrement()
    Include the class:: scope resolution bit, to make the functions part of the class.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    9
    Something along the lines of:

    Code:
    include "numbers.cpp"
    ????

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by bumcheekcity View Post
    Something along the lines of:

    Code:
    include "numbers.cpp"
    ????
    No. What salem meant was that decrement() and increment() ... are part of the class.
    you have to implement them like
    Code:
    void numbers::decrement() {
    	value--;
    }
    Kurt

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    9
    Thanks for the help. My numbers.cpp page now looks as shown, but I have a problem with calling a function within a class. I can do it in PHP, by calling $this->functionname. How is it done in C++? I gett he following errors.

    /home/bumcheekcity/Desktop/C++/euler/problem27/src/numbers.cpp:39: error: no matching function for call to ‘numbers::is_int(double)’
    /home/bumcheekcity/Desktop/C++/euler/problem27/src/numbers.cpp:26: note: candidates are: bool numbers::is_int()


    The relevent lines are the huge if statement in the is_prime() function, and the declaration:
    bool numbers::is_int() (Line 26)
    if ((!is_int(value)) || (value < 2) || (is_int(sqrt(value))) || ((int(value) &#37; 2 == 0) && (value != 2))) (Line 39)

    Code:
    #include <iostream>
    #include <math.h>
    #include "numbers.h"
    
    //Here are the two constructors, one with, one without argument.
    numbers::numbers()
    {
    	value = 0;
    }
    numbers::numbers(float initial_value)
    {
    	value = initial_value;
    }
    
    //End constructors.
    void numbers::decrement()
    {
    	value--;
    }
    
    void numbers::increment()
    {
    	value++;
    }
    
    bool numbers::is_int()
    {
    	if (int(value) == value)
    	{
    		return true;
    	} else {
    		return false;
    	}
    }
    
    bool numbers::is_prime()
    {
    	//A non-integer can't be a prime, neither can one that is less than 2, is a perfect square, or that divides by 2 and isn't two.
    	if ((!is_int(value)) || (value < 2) || (is_int(sqrt(value))) || ((int(value) % 2 == 0) && (value != 2)))
    		return false;
    	
    	//2 is a prime.
    	if (value == 2)
    		return true;
    
    	//Else, from 3, loop up the odd numbers until you get to the square root.
    	int count;
    	for (count = 3; count < sqrt(value); count = count + 2)
    	{
    		if ((int(value) % count) == 0)
    			return false;
    	}
    	return true;
    }
    
    void numbers::set_value(float new_value)
    {
    	value = new_value;
    }
    
    float numbers::show_value()
    {
    	return value;
    }

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    can do it in PHP, by calling $this->functionname.
    something like this can be done in c++ as well but is rarely necessary ( only if you pass arguments with the same name as members ).
    it would be
    Code:
    this->func();
    value is a member of the class so the statement should look like this
    Code:
    if ((!is_int()) || (value < 2) || (is_int(sqrt())) || ((int(value) % 2 == 0) && (value != 2)))
    Kurt

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    9
    The sqrt() is giving me problems, so I just dropped that. Anyway, it's working brilliantly now. Thanks very much

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by bumcheekcity View Post
    The sqrt() is giving me problems, so I just dropped that. Anyway, it's working brilliantly now. Thanks very much
    You have to link to the math library.
    Code:
    g++ problem27.cpp numbers.cpp -lm -o problem27
    I thought sqrt() was a member of the numbers class as well.
    Code:
    if ((!is_int()) || (value < 2) || (is_int(sqrt(value))) || ((int(value) % 2 == 0) && (value != 2)))
    Kurt

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, 03:39 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 02:09 PM
  3. Replies: 5
    Last Post: 06-01-2006, 05:37 PM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM
  5. TAPI "undefined reference"
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-16-2002, 09:16 PM

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