【问题标题】:Weird Compiler Errors奇怪的编译器错误
【发布时间】:2016-01-23 20:39:42
【问题描述】:

以下代码出现大量错误,我无法理解。

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): 重定位0的符号索引11无效

它们似乎都与上述相似,只是末尾的数字不同。这很可能是因为我试图从代码中删除其中一个类定义。

#include <string>
using namespace std;

static const float MAX_SATCHEL_VOLUME = 0.20; // in m^3
static const float MAX_CARTON_VOLUME = 0.50; // in m^3
static const float MAX_PALLET_VOLUME = 2.00;// in m^3

static const float SATCHEL_COST_PER_KILO = 2.00; // in dollars
static const float CARTON_COST_PER_KILO = 1.00; // in dollars
static const float PALLET_COST_PER_KILO = 0.50; // in dollars

class freight
{
    public:
        enum FreightType
        {
            SATCHEL,
            CARTON,
            PALLET,
        };
        float cost()
        {
            return perKiloCost * weight;
        }
    private:
        freight (string set_address, float set_length, float set_width, float set_height, float set_weight);
        string address;
        float length;
        float width;
        float height;
        float weight;
        FreightType type;
        float perKiloCost;
        ~freight();
};

freight::freight (string set_address, float set_length, float set_width, float set_height, float set_weight)
{
    address = set_address;
    length = set_length;
    width = set_width;
    height = set_height;
    weight = set_weight;
    type = PALLET;
    perKiloCost = 1.00;
        {
            float volume = length * width * height;
            if(volume > MAX_PALLET_VOLUME)
            {
                type = PALLET;
                perKiloCost = PALLET_COST_PER_KILO;
            }
            else if(volume > MAX_CARTON_VOLUME)
            {
                type = CARTON;
                perKiloCost = CARTON_COST_PER_KILO;
            }
            else
            {
                type = SATCHEL;
                perKiloCost = SATCHEL_COST_PER_KILO;
            }
        }
}

freight::~freight()
{

}

【问题讨论】:

  • string address; 应该是std::string address;,与参数定义相同。
  • 哇哦。完全忘记了标准...
  • 请每个问题一题。
  • 似乎一旦添加了 main(),上面的代码现在就可以编译了。

标签: c++ class enums compiler-errors


【解决方案1】:

你必须使用限定名std::string

freight(std::string set_address, float set_length, float set_width, float set_height, float set_weight):

另一个问题是你用参数定义了构造函数两次:一次在类定义内,另一次在类定义外。删除一个定义。

您还在类定义之外定义了析构函数。您首先必须至少在类定义中声明它。

【讨论】:

  • 是的,我刚刚修复了那一点。现在出现了我以前从未见过的错误。 test.cpp:64:1: 错误: 重新定义'freight::freight(std::string, float, float, float, float)' shipping::freight (string set_address, float set_length, float set_width, float set_height, float set_weight) test.cpp:22:6: error: 'freight::freight(std::string, float, float, float, float)' 之前在这里定义的运费(string set_address, float set_length, float set_width, float set_height, float set_weight): test.cpp:73:19: error: 定义隐式声明的'freight::~freight()' shipping::~freight()
  • @DwayneH 查看我更新的帖子。
  • 我修复了第二部分,这是我的疏忽。我无法看到内部类定义的开始和结束位置。这是最初对代码所做的编辑的一部分。是枚举之后到私有开始的所有内容吗?
  • @DwayneH 很抱歉,我没有理解您的问题。您还必须在类定义中包含析构函数声明(或其定义)。构造函数和枚举在您的类定义中是公开的。
  • @DwayneH 在构造函数之后有成员函数 float cost() { return perKiloCost * weight; }
【解决方案2】:

使用合格的std::string 或使用using namespace std; 传播其命名空间。

另外,你为什么要定义然后函数在类声明中和第二次在它下面

另外,您没有在类声明声明析构函数。

【讨论】:

  • 老实说,我并不完全确定。这是建议给我的代码,作为对我所做的货运等级的编辑,它不太适合我的目的,但格式似乎已关闭。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-19
  • 1970-01-01
相关资源
最近更新 更多