如何绕过不受支持的 C++11 代码
How to get around unsupported C++11 code
我学校的编译器似乎不支持 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’
你可以替换
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:
未初始化成员(静态数组除外)+默认构造函数
class lockled_ring_buffer_spsc
{
private:
int write;
int read;
int size;
string buffer[RING_BUFFER_SIZE];
//std::mutex lock;
public:
lockled_ring_buffer_spsc() : write(0),read(0),size(RING_BUFFER_SIZE)
{}
和
if (tmp._Equal("EOF"))
就是
if (tmp == "EOF")
正如有人指出的那样,std::mutex
仅在 C++ 11 中引入。例如,您必须使用纯 C pthread_mutex_lock
/ pthread_mutex_unlock
函数。
奇怪的是你的编译器没有抱怨 std::mutex
,这是一个 C++11 特性,而是抱怨其他 C++11 特性。
线条
int write = 0;
int read = 0;
int size = RING_BUFFER_SIZE;
在 C++11 中有效,但在之前的版本中无效。您可以在构造函数中初始化它们。
class lockled_ring_buffer_spsc
{
private:
int write;
int read;
int size;
string buffer[RING_BUFFER_SIZE];
std::mutex lock;
public:
lockled_ring_buffer_spsc() : write(0), read(0), size(RING_BUFFER_SIZE) {}
...
};
只要编译器在 C++11 之前的版本中支持 std::mutex
,它就可以工作。如果不是这种情况,您将不得不单独找到该问题的解决方案。
我学校的编译器似乎不支持 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’
你可以替换
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:
未初始化成员(静态数组除外)+默认构造函数
class lockled_ring_buffer_spsc
{
private:
int write;
int read;
int size;
string buffer[RING_BUFFER_SIZE];
//std::mutex lock;
public:
lockled_ring_buffer_spsc() : write(0),read(0),size(RING_BUFFER_SIZE)
{}
和
if (tmp._Equal("EOF"))
就是
if (tmp == "EOF")
正如有人指出的那样,std::mutex
仅在 C++ 11 中引入。例如,您必须使用纯 C pthread_mutex_lock
/ pthread_mutex_unlock
函数。
奇怪的是你的编译器没有抱怨 std::mutex
,这是一个 C++11 特性,而是抱怨其他 C++11 特性。
线条
int write = 0;
int read = 0;
int size = RING_BUFFER_SIZE;
在 C++11 中有效,但在之前的版本中无效。您可以在构造函数中初始化它们。
class lockled_ring_buffer_spsc
{
private:
int write;
int read;
int size;
string buffer[RING_BUFFER_SIZE];
std::mutex lock;
public:
lockled_ring_buffer_spsc() : write(0), read(0), size(RING_BUFFER_SIZE) {}
...
};
只要编译器在 C++11 之前的版本中支持 std::mutex
,它就可以工作。如果不是这种情况,您将不得不单独找到该问题的解决方案。