-1

我对 C++ 很陌生,所以关于这个主题的其他帖子对我来说有点太技术性了。当我尝试编译我的代码时,我得到一个未解决的外部符号错误。

这是我的 .h 文件:

#pragma once
#include <string>
using namespace std;

class Item 
{
private: 
  string description;
  double price;
  int weight;
  int quantity;

public:
 Item(void);
 ~Item(void); 
 Item(double OrderPrice, int OrderWeight, string Description);
 void setOrderPrice(double amount);
 void setOrderWeight(int ounces);
 void setDescription(string desc);
 void setQuantity(int number);

 int getOrderPrice();
 int getOrderWeight();
 string getDescription();
 int getQuantity();

 void show();
};

这是我的 .cpp 文件:

#include <iostream>
#include <string>
#include "Item.h"
using namespace std;

Item::Item(void)
{
}


Item::~Item(void)
{
}

void Item::setOrderPrice(double amount) {
 price = amount;
}

void Item::setOrderWeight(int ounces) {
 weight = ounces;
}

void Item::setDescription(string desc) {
 description = desc;
}

void Item::setQuantity(int number) {
 quantity = number;
}

int Item::getOrderPrice() {
 return price;
}

int Item::getOrderWeight() {
 return weight;
}

string Item::getDescription() {
 return description;
}

int Item::getQuantity() {
 return quantity;
}

void Item::show() {
 cout << price << weight << description;
}

最后包含我的主要文件:

#include <iostream>
#include <string>
#include "Item.h"
using namespace std;

int main() {
 double dTotalPrice = 0.0;
 int iTotalWeight = 0;
 Item itmMouse(24.99, 14, "Wireless Mouse");
 Item itmKeyboard(22.49, 27, "USB Keyboard");
 Item itmHDMI (24.99, 12, "HDMI Cable");
 Item itmGlasses(7.99, 7, "Reading Glasses");
 itmGlasses.setQuantity(2);
 // Show the details of the order using printDetails() 
 cout << "Here are your shopping cart contents.\n";
 itmMouse.show();
 itmKeyboard.show();
 itmHDMI.show();
 itmGlasses.show();
 // Compute the total price and total weight in this section
 dTotalPrice += itmMouse.getOrderPrice();
 dTotalPrice += itmKeyboard.getOrderPrice();
 dTotalPrice += itmHDMI.getOrderPrice();
 dTotalPrice += itmGlasses.getOrderWeight();
 iTotalWeight += itmGlasses.getOrderPrice();
 iTotalWeight += itmKeyboard.getOrderWeight();
 iTotalWeight += itmHDMI.getOrderWeight();
 iTotalWeight += itmGlasses.getOrderWeight();
 // Here we show the order details
 cout << "The price of your order is $ " << dTotalPrice << endl;
 cout << "The shipping weight is " << iTotalWeight << " ounces\n";
 cout << "That is " << iTotalWeight / 16 << " pounds\n";

 return 0;
}

我得到的错误是:

error LNK2019: unresolved external symbol "public: __thiscall       Item::Item(double,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Item@@QAE@NHV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main

请让我知道(尽可能以非技术性的术语)我做错了什么以及如何解决它。预先感谢您的帮助!对此,我真的非常感激!

4

3 回答 3

8

正如链接器所说,您在 .cpp 文件中错过了

  Item(double OrderPrice, int OrderWeight, string Description);

所以添加.cpp

  Item::Item(double OrderPrice, int OrderWeight, string Description)
  {
   // your initialization step....
  }
于 2013-07-29T10:10:54.217 回答
0

为 Item 类提供参数化构造函数,因为编译器只能提供默认构造函数(Item()),如果您需要像在 main.cpp 文件中那样创建具有默认值的对象,则必须提供参数化构造函数。

于 2013-07-29T10:24:25.950 回答
0

您从未真正定义在 Itme.h 中声明的重载构造函数

unresolved external 意味着它无法解析,或者换句话说,找不到您调用或声明的函数

外部意味着它应该在另一个 .obj 文件中的某个地方定义,Visual Studio 将每个 .cpp 文件编译到不同的 .obj 文件中,然后将它们链接在一起并感觉到你告诉它有一个名为 Item::Item... 的函数应该是一个,但是如果源文件是由您编写的,并且它是项目的一部分,这并不容易,如果它在库中,那就更难了,这意味着您没有链接正确的库(只是所以你知道,因为你会像每个人一样遇到这种情况)

要解决这个问题,只需在 Item.cpp 中添加函数定义

     Item::Item(double OrderPrice, int OrderWeight, string Description)
     {
     }
于 2017-07-21T10:55:53.050 回答