Class 尝试将两个cpp文件链接到一个标头
我试图从另一个cpp文件中的cpp文件(带有函数列表)调用函数,我使用头文件在两个cpp文件中设置函数原型,我的问题是我得到了这个LNK2019错误。我不知道我做错了什么。我在几个变体之间来回切换,根据我所读的,当前的一个似乎是最正确的。我已经为此工作了几个小时,阅读了大量的文章,但似乎没有什么可以解释这个问题,我正在使用microsoft visual studio 2012 这是头文件,Rectangle.hClass 尝试将两个cpp文件链接到一个标头,class,visual-c++,header,linker-errors,Class,Visual C++,Header,Linker Errors,我试图从另一个cpp文件中的cpp文件(带有函数列表)调用函数,我使用头文件在两个cpp文件中设置函数原型,我的问题是我得到了这个LNK2019错误。我不知道我做错了什么。我在几个变体之间来回切换,根据我所读的,当前的一个似乎是最正确的。我已经为此工作了几个小时,阅读了大量的文章,但似乎没有什么可以解释这个问题,我正在使用microsoft visual studio 2012 这是头文件,Rectangle.h #pragma once class Rectangle { private:
#pragma once
class Rectangle
{
private:
int width, height;
double gravWidth, gravHeight;
public:
Rectangle();
double getAreaBig();
double getAreaSmall();
int getPerimeter();
void getLength(int b);
void getWidth(int a);
int setLength();
int setWidth();
~Rectangle();
};
这是包含函数Rectangle.cpp的cpp文件
#include <iostream>
#include "Rectangle.h"
using namespace std;
Rectangle::Rectangle()
{
width = 1;
height = 1;
gravWidth = .5;
gravHeight = .5;
}
double Rectangle::getAreaBig ()
{
return double ((width * height) - (gravWidth * gravHeight));
}
double Rectangle::getAreaSmall()
{
return ((gravWidth) * ( gravHeight));
}
int Rectangle::getPerimeter()
{
return (2 * (width + height));
}
void Rectangle::getLength(int b)
{
height = b;
gravWidth = (1/2 * width);
}
void Rectangle::getWidth(int a)
{
width = a;
gravHeight = (1/2 * height);
}
int Rectangle::setLength()
{
return height;
}
int Rectangle::setWidth()
{
return width;
}
#包括
#包括“矩形.h”
使用名称空间std;
矩形::矩形()
{
宽度=1;
高度=1;
宽度=.5;
高度=.5;
}
双矩形::getAreaBig()
{
返回双精度((宽*高)-(宽*高));
}
双矩形::getAreaSmall()
{
返回((凹坑宽度)*(凹坑高度));
}
int Rectangle::getpermiture()
{
返回(2*(宽度+高度));
}
空矩形::getLength(int b)
{
高度=b;
凹面宽度=(1/2*宽度);
}
空矩形::getWidth(int a)
{
宽度=a;
重力高度=(1/2*高度);
}
int矩形::setLength()
{
返回高度;
}
int Rectangle::setWidth()
{
返回宽度;
}
下面是app.cpp文件,这是我得到错误的地方,斜体是错误似乎指向的地方
#include <iostream>
#include "Rectangle.h"
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <string>
#include <sstream>
#include <cstddef>
using namespace std;
int main ()
{
int const WIDTH = 10;
setprecision(2);
int length = 0;
int width = 0;
cin >> width;
cin >> length;
Rectangle garden;
Rectangle gravel;
garden.getLength(length);
garden.getWidth(width);
cout << "Length of lawn: " << garden.setLength() << "Width of lawn: " << garden.setWidth();
cout << "Cost of grass: " << garden.getAreaBig();
cout << "Length of gravel: ";
cout << "Width of gravel: ";
cout << "Cost of gavel: ";
system ("pause");
}
#包括
#包括“矩形.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
int const WIDTH=10;
设定精度(2);
整数长度=0;
整数宽度=0;
cin>>宽度;
cin>>长度;
矩形花园;
矩形砾石;
花园。getLength(长度);
花园宽度(宽度);
cout我看到的第一件事是,您没有实例化矩形。如何定义矩形
~Rectangle()
在某个地方?如果你的析构函数不打算做任何事情,你可以在头文件中提供一个空的主体
~Rectangle(){}
(以防万一,把它虚拟化,这永远是一个好习惯。)*很抱歉,我忘了注释掉一些代码,如果太难理解,我将编辑。我还了解到我没有正确调用函数,这似乎真的有助于解决问题,感谢那些回答我的人。下面的代码缓解了我的cout问题矩形garden;矩形Grade;garden.getLength(length);garden.getWidth(width);gra砾.getLength(length);gra砾.getWidth(width);我这样做了,它修复了那个特定的错误,我还意识到我没有正确调用这个函数,谢谢你*顺便提一下,我该如何使它虚拟化?