【发布时间】:2017-10-13 17:57:07
【问题描述】:
我学校的编译器似乎不支持 C++11,但我不确定如何解决这个问题。我也不确定是什么导致了最后一个编译器错误。我正在尝试在 Linux 上编译:
gcc -std=c++0x project2.cpp
系统无法识别 -std=c++11。关于如何编译它的任何想法?谢谢!
#include <thread>
#include <cinttypes>
#include <mutex>
#include <iostream>
#include <fstream>
#include <string>
#include "time_functions.h"
using namespace std;
#define RING_BUFFER_SIZE 10
class lockled_ring_buffer_spsc
{
private:
int write = 0;
int read = 0;
int size = RING_BUFFER_SIZE;
string buffer[RING_BUFFER_SIZE];
std::mutex lock;
public:
void push(string val)
{
lock.lock();
buffer[write%size] = val;
write++;
lock.unlock();
}
string pop()
{
lock.lock();
string ret = buffer[read%size];
read++;
lock.unlock();
return ret;
}
};
int main(int argc, char** argv)
{
lockled_ring_buffer_spsc queue;
std::thread write_thread([&]() {
start_timing();
string line;
ifstream myfile("p2-in.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
line += "\n";
queue.push(line);
cout << line << " in \n";
}
queue.push("EOF");
myfile.close();
stop_timing();
}
}
);
std::thread read_thread([&]() {
ofstream myfile;
myfile.open("p2-out.txt", std::ofstream::out | std::ofstream::trunc);
myfile.clear();
string tmp;
while (1)
{
if (myfile.is_open())
{
tmp=queue.pop();
cout << tmp << "out \n";
if (tmp._Equal("EOF"))
break;
myfile << tmp;
}
else cout << "Unable to open file";
}
stop_timing();
myfile.close();
}
);
write_thread.join();
read_thread.join();
cout << "Wall clock diffs:" << get_wall_clock_diff() << "\n";
cout << "CPU time diffs:" << get_CPU_time_diff() << "\n";
system("pause");
return 0;
}
编译器错误:
project2.cpp:14:14: sorry, unimplemented: non-static data member initializers
project2.cpp:14:14: error: ISO C++ forbids in-class initialization of non-const static member ‘write’
project2.cpp:15:13: sorry, unimplemented: non-static data member initializers
project2.cpp:15:13: error: ISO C++ forbids in-class initialization of non-const static member ‘read’
project2.cpp:16:13: sorry, unimplemented: non-static data member initializers
project2.cpp:16:13: error: ISO C++ forbids in-class initialization of non-const static member ‘size’
project2.cpp: In lambda function:
project2.cpp:79:13: error: ‘std::string’ has no member named ‘_Equal’
【问题讨论】:
-
与问题无关,但避免手动锁定和解锁互斥锁。遵循 RAII 范例并改用
std::unique_lock或std::lock_guard。这样一来,您就不会忘记解锁互斥锁,并且在发生异常时可以防止锁泄漏。 -
显示的错误很简单,您只需将初始化移至构造函数即可。但是您使用的是
<thread>库,它根本不存在,无法“修复” -
std::string 在任何标准中都没有名为 _Equal 的成员,您可以使用 == 运算符。
-
你试过
g++而不是gcc吗?你的 GCC 版本是多少? -
不是 gcc 重定向到 g++ 吗?似乎看到错误消息。
标签: c++ compiler-errors g++ c++03