其他一切看起来都不错,但最后仍然会出现链接错误:
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 ==========
添加VERBOSE后,我收到一个新错误:
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 ==========
当我尝试构建项目时,我遇到以下错误。发生了什么事情是我写了新的头文件(除了旧的头文件之外,现在虚拟中的一个函数。根据我正在处理的文本,不需要修改相应的.cpp文件,所以我只是通过单击源文件并选择添加现有文件从上一个项目中复制它们。然后我转到项目属性/“C / C ++”/ General并添加了.cpp文件所在的路径,但是我仍然得到了链接显示的错误:
1> ------ Build build:项目:虚函数,配置:Debug Win32 ------ 1个GT;编译... 1 GT; circle.cpp 1 GT; point.cpp 1 GT; fig10_10.cpp 1>生成代码... 1个GT;链接... 1> fig10_10.obj:错误LNK2019:函数_main中引用的未解析的外部符号“public:virtual void __thiscall Circle :: print(void)const”(?print @ Circle @@ UBEXXZ) 1> fig10_10.obj:错误LNK2019:函数_main中引用的未解析的外部符号“public:virtual void __thiscall Point :: print(void)const”(?print @ Point @@ UBEXXZ) 1> C:\ Users \ ocuk \ Documents \ C ++ \ Chapter 10 \ Virtual functions \ Debug \ Virtual functions.exe:致命错误LNK1120:2个未解析的外部 1>构建日志保存在“file:// c:\ Users \ ocuk \ Documents \ C ++ \ Chapter 10 \ Virtual functions \ Virtual functions \ Debug \ BuildLog.htm” 1>虚拟功能 - 3个错误,0个警告 ==========构建:0成功,1个失败,0个最新,0个跳过==========
//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;
}
答案 0 :(得分:2)
好吧,
Circle::print(void)
Point::print(void)
链接器找不到。这意味着
这些是虚函数,确保它们在类中的某个地方实现(如果你想要多态,那么你必须在派生类中重写它们)。纯虚函数最后可以用“= 0”声明,但在这种情况下,你必须在其中一个派生类中手动实现它。
代码后更新:
你的代码看起来很好,这里只有语法错误:
double getCircumfernce() const; //return circumfernce
阻止编译。一定要纠正它。
另外:只是为了确定..你是将每段代码放在一个单独的文件中,不是吗?如果你把整个东西放到一个.cpp文件中,你就会得到
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
以及大量其他链接错误..
其他更开放的问题:
答案 1 :(得分:1)
我可以猜到两个原因。
1)旧的程序功能打印仅在基类中定义。现在,您将此函数添加到派生类Point和Circle中。但是你忘了在cpp模块中定义它们。对于这些类
2)也许你为这些函数添加了限定符const,而旧函数没有限定符const。而且你再次忘记更改cpp模块中函数的定义。