Thread: Basic Use Of Classes - Help

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    18

    Basic Use Of Classes - Help

    I seem to be getting this error:

    Converter.obj : error LNK2019: unresolved external symbol "public: double __thiscall convert::MPH2KM(double)" (?MPH2KM@convert@@QAENN@Z) referenced in function _main

    fatal error LNK1120: 1 unresolved externals
    Converter.cpp

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include "m4th2.h"
    
    
    using std::cout;
    using std::cin;
    
    int main()
    {
        
        //Variables declared, one for the choice made by the user
    	//the other for the value entered for converting.
    	int choice;
    	double mph;
    	//double pounds;
    	//double inches;
    
    
    convert convert2;
    cout << "This Is A Converter\n";
    cout << "Menu:\n" << "1 - MPH To KM\n" << "2 - Pounds To Kilos\n" << "3 - Inches To CM\n";
    cout << "Please Enter A Choice:\n";
    cin >> choice;
    
    if (choice == 1)
    {
    cout << "Please Enter The Amount Of MPH To Convert To KM:\n";
    cin >> mph;
    cout << convert2.MPH2KM(mph);
    }
    else if (choice == 2)
    {
    cout << "Please Enter The Amount of Pounds To Convert To Kilos\n";
    }
    else 
    {
    cout << "Please Enter The Amount of Inches To Convert To CM\n"; 
    }
    
    return 0;
    }
    m4th.cpp

    Code:
    #include "stdafx.h"
    #include "m4th2.h"
    
    
    
    double MPH2KM(double mph)
    {
    double km;
    double mphconv = 1;
    km = mph * 1.609;
    
    return km;
    
    }
    m4th.h

    Code:
    #include <iostream>
    
    class convert
    {
    public:
    	double MPH2KM(double mph);
    	double P2K(double p);
    	double I2C(double i);
    
    };

    Can anyone help?

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    in m4th.cpp, don't forget the scope operator to resolve MPH2KM.

    Code:
    double convert::MPH2KM(double mph)
    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.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    74
    Yep, Mario hit it right on.

    Also, just some general advice on classes for future reference.

    1) They are meant to represent real world objects with properties (car; bank account; etc.) rather than actions (convert; jump; etc.). So creating a class called "Convert" is kinda weird where simple functions in your main file would make more sense.

    2) Also, the header ".h" and implementation file ".cpp" naming convention tends to be the same as the enclosed class. (i.e. convert.h & convert.cpp).

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    18
    Thank you very much for the tips.

    I am now getting this error though:

    Code:
    error C2761: 'double convert::MPH2KM(double)' : member function redeclaration not allowed

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not sure what is the problem, but you might want to use header guards.
    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

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    "M4th.h" needs inclusion guards. You are including it in two files and there's where the redefinition should come to play.
    Also, it doesn't seem to need the #include <iostream>

    So it should look like:
    Code:
    #ifndef M4TH_H
    #define M4TH_H
    /*
    class declaration
    */
    #endif

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Kurisu33
    1) They are meant to represent real world objects with properties (car; bank account; etc.) rather than actions (convert; jump; etc.). So creating a class called "Convert" is kinda weird where simple functions in your main file would make more sense.
    If that is meant to be a general rule of thumb, then I would disagree somewhat. classes can also represent functors (classes which overload operator() ), objects of these classes can be used in a manner very similar to functions, in which case, it would make alot of sense to name the class to reflect an ability or method.

    just to go back on topic, here's one way the OP's program could have been written with functors
    Code:
    #include <iostream>
    
    class MPHtoKPH
    {
        const double conversion_factor;
    public:
        MPHtoKPH() : conversion_factor(1.609) {}
        double operator()(double);
    };
    
    double MPHtoKPH::operator ()(double d)
    {
        return d*conversion_factor;
    }
    
    int main()
    {
        MPHtoKPH convert;
        double mph(10.0);
        std::cout<< convert(mph);
    }
    [/Overkill]
    Last edited by Bench82; 08-25-2006 at 04:15 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For your example, operator() should probably be const.
    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

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Bench82
    objects of these classes can be used in a manner very similar to functions, in which case, it would make alot of sense to name the class to reflect an ability or method.
    Your class would still be a Converter. Calling an object of class Converter "convert" looks ok to me.
    Kurt

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    48
    Could you elaborate on this? I didn't see why...

    Quote Originally Posted by anon
    "M4th.h" needs inclusion guards. You are including it in two files and there's where the redefinition should come to play.
    Also, it doesn't seem to need the #include <iostream>

    So it should look like:
    Code:
    #ifndef M4TH_H
    #define M4TH_H
    /*
    class declaration
    */
    #endif

  11. #11
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. storing derived classes in a stl container
    By *DEAD* in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2008, 07:50 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM