Basic Use Of Classes - Help

This is a discussion on Basic Use Of Classes - Help within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  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
    Portugal
    Posts
    7,383
    in m4th.cpp, don't forget the scope operator to resolve MPH2KM.

    Code:
    double convert::MPH2KM(double mph)
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    20,981
    Not sure what is the problem, but you might want to use header guards.
    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

  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
    20,981
    For your example, operator() should probably be const.
    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

  9. #9
    ZuK
    ZuK is offline
    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

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

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

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