gpt4 book ai didi

C++ 未定义对 `Vector_container::Vector_container(std::initializer_list)' 的引用

转载 作者:行者123 更新时间:2023-11-28 05:57:32 25 4
gpt4 key购买 nike

我正在尝试编译C++ 之旅 中描述“抽象类型”的第 4.3 章中的代码。但是,我收到编译器错误:

$ g++ -std=c++11 abstract.cpp Vector.cpp Vector_container.cpp
/tmp/ccToFsNS.o: In function `g()':
abstract.cpp:(.text+0x9c): undefined reference to `Vector_container::Vector_container(std::initializer_list<double>)'
abstract.cpp:(.text+0xb4): undefined reference to `Vector_container::~Vector_container()'
abstract.cpp:(.text+0xc5): undefined reference to `Vector_container::~Vector_container()'
collect2: error: ld returned 1 exit status

我怎样才能修复我的代码以便它编译?我是 C++ 的新手,正在尝试尽我所能转录 Stroustrup 的示例。

抽象.cpp:

#include "Vector_container.hpp"
#include <iostream>

void use(Container& c)
{
const int sz = c.size();
for (auto i = 0; i != sz; ++i)
{
std::cout << c[i] << "\n";
}
}

void g()
{
Vector_container vc {10, 9, 8, 7, 6};
use(vc);
}

int main()
{
g();
}

Vector_container.hpp:

#include "Container.hpp"
#include "Vector.hpp"
#include <initializer_list>

#pragma once

class Vector_container : public Container {
Vector v;
public:
Vector_container(int);
Vector_container(std::initializer_list<double>);
~Vector_container();
double& operator[](int);
int size() const;
};

Vector_container.cpp:

#include "Container.hpp"
#include "Vector.hpp"

class Vector_container : public Container {
Vector v;
public:
Vector_container(int s) : v(s) {}
Vector_container(std::initializer_list<double> lst) : v{lst} {}
~Vector_container() {}
double& operator[](int i) { return v[i]; }
int size() const { return v.size(); }
};

容器.hpp:

#pragma once

class Container {
public:
virtual double& operator[](int) = 0;
virtual int size() const = 0;
virtual ~Container() {}
};

vector .hpp:

#include <initializer_list>

#pragma once

class Vector {
public:
Vector(int);
Vector(std::initializer_list<double>);
~Vector();
double& operator[](int);
int size() const;
void push_back(double);
private:
double* elem;
int sz;
};

vector .cpp:

#include "Vector.hpp"
#include <stdexcept>

Vector::Vector(int s) : elem{new double[s]}, sz{s}
{
if (s < 0)
{
throw std::length_error{"Size must be >= 0"};
}
for (auto i = 0; i != s; ++i)
{
elem[i] = 0;
}
}

Vector::Vector(std::initializer_list<double> lst)
: elem{new double[lst.size()]}, sz{static_cast<int>(lst.size())}
{
std::copy(lst.begin(), lst.end(), elem);
}

Vector::~Vector() {
delete[] elem;
}

double& Vector::operator[](int i)
{
if (i < 0 || size() <= i)
{
throw std::out_of_range{"Vector::operator[]"};
}
return elem[i];
}

int Vector::size() const
{
return sz;
}

最佳答案

正如评论者和 Sam 所指出的,我没有正确定义 Vector_container。我将 Vector_container.cpp 更改为:

#include "Vector_container.hpp"

Vector_container::Vector_container(int s) : v(s) {}
Vector_container::Vector_container(std::initializer_list<double> lst) : v{lst} {}
Vector_container::~Vector_container() {}

double& Vector_container::operator[](int i)
{
return v[i];
}
int Vector_container::size() const
{
return v.size();
}

现在程序按预期编译和运行。

关于C++ 未定义对 `Vector_container::Vector_container(std::initializer_list<double>)' 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33847833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com