Bazman
Bazman

Reputation: 2150

Linking Error in C++ using Visual Studio 2008

Everything else looks fine but still getting linking errors at the end:

1>Finished searching libraries
1>Finished pass 1
1>Generating non-SAFESEH image.
1>Invoking CVTRES.EXE:
1> /machine:x86
1> /verbose
1> /out:"C:\Users\ocuk\AppData\Local\Temp\lnk9464.tmp"
1> /readonly
1> ".\Debug\Virtual functions.exe.embed.manifest.res"
1>Microsoft (R) Windows Resource To Object Converter Version 9.00.21022.08
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>adding resource. type:MANIFEST, name:1, language:0x0409, flags:0x30, size:2048
1>fig10_10.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall Circle::print(void)const " (?print@Circle@@UBEXXZ) referenced in function _main
1>fig10_10.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall Point::print(void)const " (?print@Point@@UBEXXZ) referenced in function _main
1>C:\Users\ocuk\Documents\C++\Chapter 10\Virtual functions\Debug\Virtual functions.exe : fatal error LNK1120: 2 unresolved externals
1>Build log was saved at "file://c:\Users\ocuk\Documents\C++\Chapter 10\Virtual functions\Virtual functions\Debug\BuildLog.htm"
1>Virtual functions - 3 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

After adding VERBOSE I am getting a new error:

1>------ Rebuild All started: Project: Virtual functions, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'Virtual functions', configuration 'Debug|Win32'
1>Compiling...
1>circle.cpp
1>fig10_10.cpp
1>point.cpp
1>Generating Code...
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>Linking...
1>LINK : fatal error LNK1104: cannot open file 'VERBOSE.obj'
1>Build log was saved at "file://c:\Users\ocuk\Documents\C++\Chapter 10\Virtual    functions\Virtual functions\Debug\BuildLog.htm"
 1>Virtual functions - 1 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

I'm getting the following errors when I try to build my project. What has happened is that I have written new header files (which are the same as the old ones except one of the functions in now virtual. According to the text I am working from no modifications to the corresponding .cpp files are necessary so I simply copied them from the previous project, by clicking on source files and choosing add existing. I then went to the project properties/"C/C++"/General and added the path where the .cpp files are located, however I am still getting the linking errors shown:

enter image description here 1>------ Build started: Project: Virtual functions, Configuration: Debug Win32 ------ 1>Compiling... 1>circle.cpp 1>point.cpp 1>fig10_10.cpp 1>Generating Code... 1>Linking... 1>fig10_10.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall Circle::print(void)const " (?print@Circle@@UBEXXZ) referenced in function _main 1>fig10_10.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall Point::print(void)const " (?print@Point@@UBEXXZ) referenced in function _main 1>C:\Users\ocuk\Documents\C++\Chapter 10\Virtual functions\Debug\Virtual functions.exe : fatal error LNK1120: 2 unresolved externals 1>Build log was saved at "file://c:\Users\ocuk\Documents\C++\Chapter 10\Virtual functions\Virtual functions\Debug\BuildLog.htm" 1>Virtual functions - 3 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

//Fig 10.9:circle.h
 //Circle class contains x-y coordinate pair and radius

#ifndef CIRCLE_H
#define CIRCLE_H

#include "point.h"      //Point class definition

class Circle: public Point{

public:
//default constructor
Circle(int=0, int=0, double=0.0);

void setRadius( double);        //set radius
double getRadius() const;       //return radius

double getDiameter() const;     //return diameter
double getCircumfernce() const; //return circumfernce
double getArea() const;         //return area

virtual void print() const;     //output Circle object

private:
double radius;      //Circle's radius
};

#endif

//Fig. 10.8:point.h
//Point class definition represents an x-y coordinate pair
#ifndef POINT_H
#define POINT_H

class Point{

public:
Point(int=0, int=0);        //default constructor

void setX(int);     //setX in coordinate pair
int getX() const;   //return x in coordinate pair

void setY(int);     //set y in coordinate pair
int getY() const;   //return y from coordinate pair

virtual void print() const;     //output Point object

private:
int x;      //x part of coordinate pair
int y;      //y part of coordinate pair

};


#endif


//Fig 10.4: circle.cpp
// Circle class member-function definitions

#include <iostream>

using std::cout;

#include "circle.h"     //Cicle class definition

//default constructor
Circle::Circle(int xValue, int yValue, double radiusValue):Point (xValue, yValue)//call   base class constructor
{
setRadius(radiusValue);
}//end circle constructor

//set Radius
void Circle::setRadius(double radiusValue)
 {
radius=(radiusValue<0.0?0.0:radiusValue);
}//end fuinction setRadius

//return radius
double Circle::getRadius() const
{
return radius;
}//end finction getRadius

//calculate and return diameter
double Circle::getDiameter() const
{
return 2*getRadius();
}

//calculate and return diameter
double Circle::getCircumference() const
{
return 3.14159*getDiameter();
}

//calculate and return area
double Circle::getArea() const
{
return 3.14159*getRadius()*getRadius();
}//end function getArea

//output Circle object
void Circle::print() const
{
cout << "center = ";
Point::print();     //invoke Point's print function
cout << "; radius = " << getRadius();
}//end function print


//Fig. 10.2:point.cpp
//Point class member function definitions
#include <iostream>

using std::cout;

#include "point.h"      //Point clss deinition

//default constructor
Point::Point(int xValue, int yValue):x(xValue),y(yValue)
{
//empty body
}

//set x in coordinate pair
void Point::setX( int xValue)
{
x=xValue;   //no need for validation
}//end function setX


//return x from coordinate pair
int Point::getX() const
{
return x;
}//end function getX

//set y in coordinate pair
void Point::setY( int yValue)
{
y=yValue;   //no need for validation
}//end function setY


//return y from coordinate pair
int Point::getY() const
{
return y;
}//end function getY

//output Point object
void Point::print() const
{
cout << '[' << getX() << ", " << getY() << ']';

} //end function print


 //Fig. 10.10: fig10_10.cpp
//Introducing polymorphism, virtual functions and dynamic binding

#include <iostream>
using std::cout;
using std::endl;
using std::fixed;

#include <iomanip>

using std::setprecision;

#include "point.h"      //Point class definition
#include "circle.h"     //Circle class definition

int main()
{
Point point(30,50);
Point *pointPtr=0;

Circle circle(120,89,2.7);
Circle *circlePtr=0;

//set floating point precision
cout << fixed <<setprecision(2);

//output objects point and circle using static binding
cout << "Invoking print function on point and circle "
    << "an objects with static binding "
    << "ananPoint: ";
point.print();  //static binding
cout << "anCircle: ";
circle.print(); //static binding

//output pbjects print and cicrle using dynamic binding
cout << "\n\nInvoking print function on point and circle "
    << "\nobjects with dynamic binding";

//aim base-class pointer at base-class object and print
pointPtr=&point;
cout << "\n\nCalling virtual function print with base-class"
    << "\npointer to  a base-class object"
    <<"\ninvokes base-class print function:\n";
pointPtr->print();

//aim derived calss pointer at derived class
//object and print
circlePtr=&circle;
cout << "\n\nCalling virtual function print with "
    <<"\nderived-class pointer to derived class object "
    <<"\ninvokes derived class print function:\n";
circlePtr->print();

//aim-base calss pointer at derive class object and print
pointPtr=&circle;
cout << "\n\nCalling virtual function print with base-class"
    << "\npointer to derived class object "
    <<"\ninvokes derived class print function:\n";
pointPtr->print();      //polymorphiosm invokes ciecle's print
cout << endl;

return 0;

}

Upvotes: 0

Views: 850

Answers (2)

Marco A.
Marco A.

Reputation: 43662

Well,

Circle::print(void)
Point::print(void)

aren't being found by the linker. That means either

  • The code for a function isn't being generated
  • You forgot a .lib file to link against (check project properties->Linking->Input for additional dependencies)

Those are virtual functions, make sure they're being implemented somewhere in your class (and if you want polymorphism then you'll have to override them in a derived class). A pure virtual function can be declared with " = 0 " at the end, but in that case you'll have to implement it manually in one of the derived classes.


Update after code:

your code looks fine to me, there's only a grammar error here:

double getCircumfernce() const; //return circumfernce

which prevents compilation. Make sure to correct it.

Also: just to be sure.. are you putting each piece of code into a separate file, aren't you? If you put this whole thing into a single .cpp file you will get

1>------ Build started: Project: ConsoleApplication2, Configuration: Debug Win32 ------
1>  ConsoleApplication2.cpp
1>ConsoleApplication2.obj : error LNK2005: "public: __thiscall Circle::Circle(int,int,double)" (??0Circle@@QAE@HHN@Z) already defined in circle.obj
1>ConsoleApplication2.obj : error LNK2005: "public: __thiscall Point::Point(int,int)" (??0Point@@QAE@HH@Z) already defined in point.obj

and tons of other linking errors..

Other more exoteric issues:

  • Make sure things like anti-viruses or defenders aren't getting in the way for the compilation process (they might stop MS linker from accomplishing its tasks)
  • Make sure you have read/write permissions (or run VS as administrator)
  • Make sure all .obj files (circle.obj, point.obj and fig10_10.obj are found by VS by enabling the /VERBOSE compilation flag as described here: http://msdn.microsoft.com/en-us/library/wdsk6as6(v=vs.110).aspx

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 310910

I can guess about two reasons.

1)In the old your program function print was defined only in the base class. Now you added this function to your derived classes Point and Circle. But you forgot to define them in the cpp module. for these classes

2) Maybe you added qualifier const for these functions while the old functions have no qualifier const. And again you forgot to change definitions of the functions in the cpp module.

Upvotes: 1

Related Questions