Linking error!

This is a discussion on Linking error! within the C++ Programming forums, part of the General Programming Boards category; Why I am getting a linking error with this code? one compiler printed this: " [Linker error] undefined reference to ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    33

    Linking error!

    Why I am getting a linking error with this code?

    one compiler printed this:
    "
    [Linker error] undefined reference to `box::box(float, float, float, float, float, basic_string<char, string_char_traits<char>, __default_alloc_template<false, 0> >, basic_string<char, string_char_traits<char>, __default_alloc_template<false, 0> >)'
    "

    Here is what the other compiler printed to me:
    "--------------------Configuration: assign5p2 - Win32 Debug--------------------
    Compiling...
    assign5p2.cpp
    C:\Windows\assign5p2.cpp(122) : warning C4305: 'argument' : truncation from 'const double' to 'float'
    C:\Windows\assign5p2.cpp(122) : warning C4305: 'argument' : truncation from 'const double' to 'float'
    C:\Windows\assign5p2.cpp(122) : warning C4305: 'argument' : truncation from 'const double' to 'float'
    C:\Windows\assign5p2.cpp(122) : warning C4305: 'argument' : truncation from 'const double' to 'float'
    C:\Windows\assign5p2.cpp(119) : warning C4101: 't' : unreferenced local variable
    C:\Windows\assign5p2.cpp(119) : warning C4101: 'w' : unreferenced local variable
    C:\Windows\assign5p2.cpp(119) : warning C4101: 'lb' : unreferenced local variable
    C:\Windows\assign5p2.cpp(119) : warning C4101: 'h' : unreferenced local variable
    C:\Windows\assign5p2.cpp(119) : warning C4101: 'l' : unreferenced local variable
    Linking...
    assign5p2.obj : error LNK2001: unresolved external symbol "public: __thiscall box::box(float,float,float,float,float,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::cha
    r_traits<char>,class std::allocator<char> >)" (??0box@@QAE@MMMMMV?$basic_string@DU?$char_traits@ D@std@@V?$allocator@D@2@@std@@0@Z)
    Debug/assign5p2.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    "

    assign5p2.exe - 2 error(s), 9 warning(s)


    Code:
    /*Purpose: This program creates a class named box. The class contains the variables
    of the box and also the operations on it.  This includes the function that sets the size
    and prints information about the box.  The values and changed and manipulated throughout the
    program. */
    
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    
    class box
    {
    
    //member function prototypes
    public:
    void set_size(float,float,float,float,float,string,string);//changes box variables
    //the variables are length,width,height,thickness,weight,color,contents (yes or no)
    void get_size();                   // returns dimensions of the box
    
    void re_enter();
    void calc_area();     // calculates a box's outside area
    void calc_volume();   // calculates a box's volume
    void print_report(); // prints all known info about a box
    box(float,float,float,float,float,string,string); //constructor
    box();                                            //default constructor
                                
    
    	
    //member variables
    private:
    float length;             // length of the box (ft)
    float width;              // width of the box (ft)
    float height;              // height of the box (ft)
    float thickness;           // thickness of the box material (in)
    float weight;             // weight of box (lb)
    string color;              // color of the box
    float volume;             //volume of the box
    float areaofsideone,areaofsidetwo,areaofsidethree;//area of three different sides 
    float surfacearea;                                // surface area
    string contents;           // whether it contains something (yes or no)
    
    
    };
    
    
    
    void box::calc_volume()
    //Precondition: Length, width, and height have been assigned.
    //Postcondition: Returns volume of box with variables in parameter list.
    {
    	
    	volume=length*width*height;
    }
    
    void box::calc_area()
    //Precondition: Length, width, and height have been assigned.
    //Postcondition: Returns surface area of box with variables in parameter list.
    {
    	areaofsideone=height*length;
    	areaofsidetwo=width*length;
        areaofsidethree=height*width;
    	surfacearea=2*areaofsideone+2*areaofsidetwo+2*areaofsidethree;
    }
    	
    
    void box::get_size()
    //This function returns a box's current dimensions
    {
    	cout<<"The box's current dimensions are: length="<<length<<", width="
    		<<width<<", and height="<<height<<endl;
    
    }
    
    void box::set_size(float l,float w,float h,float t,float lb,string c,string cont)
    //This function changes a box's dimensions and also its other variables
    {
    	if(l<=0||w<=0||h<=0||t<=0||lb<=0) //checking if valid input was entered (>0)
    	{re_enter();}
    	
    	length=l;
    	width=w;
    	height=h;
    	thickness=t;
    	weight=lb;
    	color=c;
    	contents=cont;
    }
    
    void box::re_enter()
    {
    	float l,w,h,t,lb; //length,width,height,thickness,weight
        string c,cont;   //color, contents (yes or no)
    	cout<<"The input you entered is invalid."<<endl;
        cout<<"Please re-enter your data as valid inputs: "<<endl;
    	cin>>l>>w>>h>>t>>lb>>c>>cont;
    	set_size(l,w,h,t,lb,c,cont);
    }
    
    
    void box::print_report()
    //This function prints all known info. about the box
    {	
    	cout<<"The box's current dimensions are: length="<<length<<", width="
    		<<width<<", and height="<<height<<endl;
    	cout<<"Other box properties include:"<<endl
    		<<"thickness="<<thickness<<endl
    		<<"weight="<<weight<<endl
    		<<"surface area="<<surfacearea<<endl
    	    <<"area of three different sides:"<<areaofsideone<<", "<<areaofsidetwo<<", "
    		<<areaofsidethree<<endl
    	    <<"volume="<<volume<<endl
    		<<"color is "<<color<<endl
            <<"contents? "<<contents<<endl<<endl;
    }
    
    int main()
    {
       float l,w,h,t,lb; //length,width,height,thickness,weight
       string c,cont;   //color, contents (yes or no)
       //box box1;        //name of the first box
       box box1(1.5,3.4,0.9,0.1,2.7,"brown","no");
       box1.calc_area();
       box1.calc_volume();
       box1.print_report();
       //just testing to see if I get some output going before I write the rest of the program
    
    
    
    return 0;
    
    }
    Last edited by m712; 12-08-2002 at 09:00 PM.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    because you declared a constructor with a bunch of arguments and called it, but you never defined what the constructor does.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    33
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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