我只是为自己找到了“ cmake”。现在尝试将其实施到我的一些旧程序中。但是现在,我在用cmake编译时遇到此错误:
cd项目目录
cmake。
make-并在此处引发错误“在'&&'标记TString(TString && __ obj);之前的','或'...'预期”
但是,如果我使用以下程序编译该程序:
g ++ program.cpp -o program -std = c ++ 11-它可以正确编译而不会出现错误。
我在Ubuntu(gcc版本5.4.0 20160609(Ubuntu 5.4.0-6ubuntu1〜16.04.10)) 我不太清楚怎么了。 这是我的代码:
string.cpp
#include <iostream>
#include <string>
#include <cstring>
#include "TString.h"
using namespace std;
int main(int argc, char const *argv[]) {
long n;
std::cout << "\n Enter num to initialize binstring" << '\n';
std::cin >> n;
TBinString bin(n);
std::cout << "\n Your number in bin" << '\n';
TBinString bin2(bin);
std::cout << bin2.GetString() << '\n';
bin2 ="101";
std::cout << "\nAdding 5 to your number" << '\n';
bin += bin2;
std::cout << bin.GetString() << '\n';
system("pause");
return 0;
}
TString.h
#include <iostream>
#include <cstring>
class TString {
protected:
char *str;
int size;
inline void eat_rest();
public:
TString();
TString(int);
TString(const char*);
TString(TString &_obj);
TString(TString &&_obj);
~TString();
void Set(const char*);
const char* GetString() const;
int GetSize() const;
int Len();
void Clear();
void operator=(const TString &_obj);
void operator+=(const TString &_obj);
void operator+=(const char*);
TString operator+(const TString &_obj);
TString operator+(const char*);
friend bool operator == (const TString &_left, const TString &_right);
friend bool operator != (const TString &_left, const TString &_right);
};
class TBinString : public TString{
public:
TBinString();
TBinString(long);
TBinString(const char*);
TBinString(TBinString &);
TBinString(TBinString &&);
~TBinString();
void Sign();
int FindBits(long);
TBinString Sum(const char*);
void operator=(const TBinString &_obj);
TBinString operator+(TBinString &);
TBinString operator+(const char*);
TBinString operator+(const long);
void operator+=(const TString &);
void operator+=(const char*);
void operator+=(const long);
};
TString.cpp
#include "TString.h"
TString::TString(int n){
this->str = new char[n];
this->size = n;
}
TString::TString():TString(80){}
TString::TString(const char* str):TString(strlen(str)+1){
//strncpy(this->str, str, this->size);
strcpy_s(this->str, this->size, str);
}
TString::TString(TString &_obj):TString(_obj.size){
//strncpy(this->str, _obj.str, this->size);
strcpy_s(this->str, this->size, _obj.str);
}
TString::TString(TString &&_obj){
this->str = _obj.str;
_obj.str = NULL;
this->size = _obj.size;
_obj.size = 0;
}
TString::~TString(){
delete [] str;
}
void TString::Set(const char *_str){
delete [] this->str;
this->str = new char[strlen(_str)+1];
this->size = strlen(_str)+1;
//strncpy(this->str, _str, this->size);
strcpy_s(this->str, this->size, _str);
}
const char* TString::GetString() const{
if (this->str == NULL) {
return "<string is empty>";
}
return this->str;
}
int TString::GetSize() const{
return this->size;
}
int TString::Len(){
return strlen(this->str);
}
void TString::Clear(){
this->Set("");
}
void TString::operator=(const TString &_obj){
this->Set(_obj.str);
}
TString TString::operator+(const TString &_obj){
char *buff = new char[strlen(this->str) + strlen(_obj.str) + 1];
size_t size = strlen(this->str) + strlen(_obj.str) + 1;
buff[0] = 0;
//strncat(buff, this->str, size);
strcat_s(buff, size, this->str);
//strncat(buff, _obj.str, size);
strcat_s(buff, size, _obj.str);
TString ret(buff);
return ret;
}
TString TString::operator+(const char *_obj){
char *buff = new char[strlen(this->str) + strlen(_obj) + 1];
size_t size = strlen(this->str) + strlen(_obj) + 1;
buff[0] = 0;
//strncat(buff, this->str, size);
strcat_s(buff, size, this->str);
//strncat(buff, _obj.str, size);
strcat_s(buff, size, _obj);
TString ret(buff);
return ret;
}
void TString::operator+=(const TString &_obj){
*this = *this + _obj;
}
void TString::operator+=(const char *_obj){
*this = *this + _obj;
}
bool operator == (const TString &_left, const TString &_right){
if (_left.GetSize() == _right.GetSize()) {
for (size_t i = 0; i < _left.GetSize(); i++) {
if (_left.GetString()[i] != _right.GetString()[i])
return false;
}
return true;
}else
return false;
}
bool operator!=(const TString &_left, const TString &_right){
return !(_left == _right);
}
void TString::eat_rest(){
while (std::cin.get() != '\n') ;
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// class TBinString
TBinString::TBinString(){
this->str = NULL;
this->size = 0;
}
TBinString::TBinString(long n):TBinString(){
this->size = this->FindBits(n) +2; //one for sign and one fo \0
this->str = new char[this->size];
this->str[0] = '0';
long m = abs(n);
for (size_t i = this->size-2; i > 0; i--) {
this->str[i] = (m % 2) + '0';
m /= 2;
}
this->str[this->size-1] = '\0';
if (n < 0)
this->Sign();//доп. код
}
TBinString::TBinString(const char* _str):TBinString(){
for (size_t i = 0; i < strlen(_str); i++) {
if (_str[i] != '1' && _str[i] != '0') {
this->str = NULL;
this->size = 0; //one for \0
return;
}
}
this->size = strlen(_str) +1; //one for \0
this->str = new char[this->size];
//strncpy(this->str, _str, this->size);
strcpy_s(this->str, this->size, _str);
}
TBinString::TBinString(TBinString &_obj):TBinString(){
this->size = _obj.size; //one for \0
this->str = new char[this->size];
//strncpy(this->str, _obj.str, this->size);
strcpy_s(this->str, this->size, _obj.str);
}
TBinString::TBinString(TBinString &&_obj):TBinString(){
this->size = _obj.size; //one for \0
this->str = _obj.str;
_obj.str = NULL;
}
TBinString::~TBinString(){}
int TBinString::FindBits(long n){
int ret = 0;
n = abs(n);
while (n > 0) {
n = n /2; ret++;
}
return ret;
}
void TBinString::Sign(){
this->str[0] = '1';
//inverting
for (size_t i = this->size-2; i > 0; i--) {
if (this->str[i] == '0')
this->str[i] = '1';
else
this->str[i] = '0';
}
//adding 1
for (size_t i = this->size-2; i > 0; i--) {
if (this->str[i] == '0'){
this->str[i] = '1';
break;
}
else
this->str[i] = '0';
}
}
TBinString TBinString::Sum(const char *_str){
bool over = false;
int size_left = this->size;
int size_right = strlen(_str)+1;
TBinString buff;
buff.size = size_left > size_right ? size_left : size_right;
size_left -= 2; size_right -= 2;
int i = buff.size-2;
buff.str = new char[buff.size];
for (;size_left >= 0 && size_right >= 0; i--, size_left--, size_right--) {
if (this->str[size_left] == '1') {
if (_str[size_right] == '0'){
if (over)
buff.str[i] = '0';
else
buff.str[i] = '1';
}
else{//_str[size_right] == '1'
if (over)
buff.str[i] = '1';
else{
buff.str[i] = '0';
over = true;
}
}
}else{//this->str[size_left] == '0'
if (_str[size_right] == '0') {
if (over) {
buff.str[i] = '1';
over = false;
}
else
buff.str[i] = '0';
}else{//_str[size_right] == '1'
if (over)
buff.str[i] = '0';
else
buff.str[i] = '1';
}
}
}
while (size_left >= 0) {
if (this->str[size_left] == '1') {
if (over)
buff.str[i] = '0';
else
buff.str[i] = '1';
}else{//this->str[size_left] == '0'
if (over) {
buff.str[i] = '1';
over = false;
}else
buff.str[i] = '0';
}
size_left--; i--;
}
while (size_right >= 0) {
if (_str[size_right] == '1') {
if (over)
buff.str[i] = '0';
else
buff.str[i] = '1';
}else{//_str[size_right] == '0'
if (over) {
buff.str[i] = '1';
over = false;
}else
buff.str[i] = '0';
}
size_right--; i--;
}
buff.str[buff.size-1] = '\0';
return buff;
}
void TBinString::operator=(const TBinString &_obj){
this->Set(_obj.str);
}
TBinString TBinString::operator+(TBinString &_obj){
return this->Sum(_obj.GetString());
}
TBinString TBinString::operator+(const char *_str){
return this->Sum(_str);
}
TBinString TBinString::operator+(const long _var){
TBinString tmp(_var);
return this->Sum(tmp.GetString());
}
void TBinString::operator+=(const TString &_obj){
*this = this->Sum(_obj.GetString());
}
void TBinString::operator+=(const char *_str){
*this = this->Sum(_str);
}
void TBinString::operator+=(const long _var){
TBinString tmp(_var);
*this = this->Sum(tmp.GetString());
}
答案 0 :(得分:0)
在写问题时得到它。抱歉! 使用cmake编译时,您需要添加standart c ++ 11标志