Thread: Simple function call in main not working ( Classes)

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    Simple function call in main not working ( Classes)

    So I have 2 files linked: Fraction.h and main.cpp

    Fraction.h contains function definitions and declarations

    main.cpp is where int main is:


    Code:
    Class Fraction
    int a;
    public:                                                     
    Fraction(int a, int b) // initialized constructor            
    { 
         a= 0;
     }       
              int get ()        
             {   
             return (a+1);
            }
    Code:
    #include "Fraction.h"
    int main ()
    {
    int a,b;
    cin >> a>>b;
    Fraction test (a,b);
    cout << test. get() ;
    }
    
    
    error: undefined reference to Fraction :: Fraction (int, int)


    In the header file, i declared int a and set it to 0. When i call the object test.get() in main, the result should be 1. Instead it's giving me the error: I think its saying somehow my constructor is undefined? But i dont see where. It's such a simple program.
    Last edited by tmac619619; 04-25-2014 at 10:50 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This compiles for me:
    Code:
    #include <iostream>
    
    class Fraction
    {
    public:
        Fraction(int a, int b)
        {
            a = 0;
        }
    
        int get()
        {
            return a + 1;
        }
    private:
        int a;
    };
    
    int main ()
    {
        using namespace std;
        int a, b;
        cin >> a >> b;
        Fraction test(a, b);
        cout << test.get();
    }
    Your error sounds like a link error though. You need to post your actual code, not a retyped version that should not even compile.
    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

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Oh ok it works when i use a makefile.

    Any ideas as to why it prints out junk values?

    input: 1,2

    output: -2113342424
    Last edited by tmac619619; 04-25-2014 at 11:21 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The member access is private by default, so it makes no difference in this case, except that I prefer to declare the public interface first where feasible.
    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

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Laserlight already mentioned this but OP, how are you compiling your code?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tmac619619
    Any ideas as to why it prints out junk values?
    You decided to name your constructor parameter with the same name as a member variable. Therefore, when you assigned to a in the constructor body, you were assigning to the parameter, not the member variable.
    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
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by MutantJohn View Post
    Laserlight already mentioned this but OP, how are you compiling your code?

    Im using Codeblocks. Weird, i placed all of my files into a console application project folder but somehow the linking gets fcked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-04-2013, 08:25 AM
  2. Using classes in function outside main
    By Bladeforger in forum C++ Programming
    Replies: 6
    Last Post: 08-10-2008, 07:36 PM
  3. function call not working
    By swgh in forum C++ Programming
    Replies: 10
    Last Post: 06-02-2005, 12:12 PM
  4. Function call not working
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2003, 07:36 PM
  5. Can I call the main () function within itself ?
    By pritesh in forum C Programming
    Replies: 9
    Last Post: 11-25-2001, 06:11 PM