gpt4 book ai didi

c++ - 尝试编译队列时出现 static_initialization_and_destruction 错误

转载 作者:行者123 更新时间:2023-11-28 04:51:14 30 4
gpt4 key购买 nike

<分区>

我正在尝试实现一个读写队列。

我的头文件是这样的:

#ifndef CIRCULARSPSC_H
#define CIRCULARSPSC_H

#include <atomic>
#include <cstddef>

template<typename Data, size_t Size>
class CircularSpsc
{
public:
enum { queue_size = Size + 1 };

CircularSpsc() : back(0), front(0){}
virtual ~CircularSpsc() {}

bool push_back(const Data& item);
bool pop_front(Data& item);

bool isEmpty() const;
bool isFull() const;

private:
size_t increment(size_t idx) const;
std::atomic<size_t> back;
Data dataArray[queue_size];
std::atomic<size_t> front;
};


#endif

这是我的源文件:

#include <circularspsc.h>


template<typename Data, size_t Size>
bool CircularSpsc<Data, Size>::push_back(const Data& item)
{
const auto current_back = back.load(std::memory_order_relaxed);

const auto next_back = increment(current_back);
if(next_back != front.load(std::memory_order_acquire))
{
dataArray[current_back] = item;
back.store(next_back, std::memory_order_release);
return true;
}
return false;
}

template<typename Data, size_t Size>
bool CircularSpsc<Data, Size>::pop_front(Data& item)
{
const auto currentfront = front.load(std::memory_order_relaxed);
if(currentfront == back.load(std::memory_order_acquire))
return false;

item = dataArray[currentfront];
front.store(increment(currentfront), std::memory_order_release);
return true;
}

template<typename Data, size_t Size>
bool CircularSpsc<Data, Size>::isEmpty() const
{
return (front.load() == back.load());
}

template<typename Data, size_t Size>
bool CircularSpsc<Data, Size>::isFull() const
{
const auto nextback = increment(back.load());
return (nextback == front.load());
}

template<typename Data, size_t Size>
size_t CircularSpsc<Data, Size>::increment(size_t idx) const
{
return (idx + 1) % queue_size;
}

在我的主文件中,当我尝试创建上面实现的队列实例时,如下所示:

#include <iostream>
#include "circularspsc.h"

int main()
{
CircularSpsc<bool, 100000> cspsc_q;

}

我收到以下错误:

    /tmp/ccQFNFP5.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x53): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x62): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccQFNFP5.o: In function `circular_spsc_relaxed::CircularSpsc<bool, 100000ul>::~CircularSpsc()':
main.cpp:(.text._ZN21circular_spsc_relaxed12CircularSpscIbLm100000EED0Ev[_ZN21circular_spsc_relaxed12CircularSpscIbLm100000EED5Ev]+0x20): undefined reference to `operator delete(void*)'
/tmp/ccQFNFP5.o:(.rodata._ZTIN21circular_spsc_relaxed12CircularSpscIbLm100000EEE[_ZTIN21circular_spsc_relaxed12CircularSpscIbLm100000EEE]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
collect2: error: ld returned 1 exit status

如果我从队列实现中删除虚拟析构函数,我仍然会收到以下两个错误:

`__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x53): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x62): undefined reference to `std::ios_base::Init::~Init()'

我正在编译:

gcc -std=c++11 -o testQueue circularspsc.cpp main.cpp

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