刚开始学习一些cpp并得到了这些东西:
#include <string>
using std::string;
class Vigenere{
public:
Vigenere(string key, string alphabet = "abcdefghijklmnopqrstuvwxyz");
string encode(string message, string key = _key, string alphabet = _alphabet);
string decode(string message, string key = _key, string alphabet = _alphabet);
private:
string _alphabet;
string _key;
};
Run Code Online (Sandbox Code Playgroud)
在尝试编译时说"10 [错误]无效使用非静态数据成员'Vigenere :: _ key'";
第10行是字符串Key;
那么,是否有一种方法可以实现它,以便我可以将这些变量分别用于每个对象,同时将它们用作默认参数?
我有一些代码,我正在编译嵌入式平台.当我尝试针对代码运行基于Clang的工具时,我遇到了一些错误.
我已将生成错误的代码缩小到以下内容:
typedef int _GCC_ATTR_ALIGN_8t __attribute__((__mode__(__QI__)));
typedef _GCC_ATTR_ALIGN_8t _Int8t __attribute__((__aligned__(1)));
typedef _Int8t _Intleast8t;
typedef _Int8t _Intfast8t;
typedef _Int8t _int8;
typedef _Int8t int8_t;
Run Code Online (Sandbox Code Playgroud)
运行clang 3.8.0对代码的输出是:
x.cpp:5:16: error: cannot combine with previous 'type-name' declaration
specifier
typedef _Int8t _int8;
^
x.cpp:5:1: warning: typedef requires a name [-Wmissing-declarations]
typedef _Int8t _int8;
^~~~~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
Run Code Online (Sandbox Code Playgroud)
为什么它仅在_int8 typedef而不是其他typedef上有问题?此外,错误是什么意思?
我有以下代码行
SystemFactory::system_ptr system = _Factory->createSystem(systemType);
_Systems.push_back(std::move(system));
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我不能只返回系统,因为它在移动后会为NULL.我带来的解决方案如下,我不知道它是否是最好的解决方案.
return (_Systems.end() - 1)->get();
Run Code Online (Sandbox Code Playgroud)
如果有更好的方法吗?
是否可以使用具有不同功能名称的两个函数,但相同的功能共享函数体?我们怎么做呢?
template<typename _T>
class array {
public:
_T operator+(_T concatinate_operand); // concatinate to the array
_T append(_T concatinate_operand);
};
Run Code Online (Sandbox Code Playgroud) 在python中,我们可以在IDE中输入help(“Keywords”)来显示所有保留字,但是,我可以输入与C++中相同的命令来显示所有保留字吗?或者我应该做什么?
我正在尝试对代码进行一些重构,并遇到了问题.该程序有一个数据管理器,它返回指向结构数组的指针作为void*.其中一种新类型的数据,而不是指向结构数组的单个指针,有两个指向数组的指针.问题是所有处理代码都是通过访问所有记录类型共有的array [index] .qwTimestamp和array [index] .snSample来完成的.
我认为像下面这样覆盖数组访问运算符([])可能会解决问题:
class ADRec {
public:
ADRec(unsigned __int64* ts, __int32* data, unsigned index = 0): mTimestamps(ts), mDataPoints(data), mIndex(index) {
qwTimeStamp = mTimestamps[mIndex];
snSample = mDataPoints[mIndex];
}
ADRec operator[](unsigned i) {
return ADRec(mTimestamps, mDataPoints, i);
}
unsigned __int64 qwTimeStamp;
__int32 snSample;
private:
unsigned __int64* mTimestamps;
__int32* mDataPoints;
unsigned mIndex;
};
Run Code Online (Sandbox Code Playgroud)
如果您使用对象,此方法可以正常工作:
unsigned __int64 ts[] = { 2, 3, 4, 5};
__int32 data[] = {4, 6, 8, 10};
ADRec tmp = ADRec(ts, data, 0);
ASSERT(tmp[0].qwTimeStamp == 2);
ASSERT(tmp[0].snSample …Run Code Online (Sandbox Code Playgroud) 我有一个矩阵类,如下所示:
template <size_t M, size_t N, typename T>
class Matrix
{
public:
Matrix<M, N, T> operator +(const Matrix<M, N, T>& B) const;
template <size_t P> Matrix<M,P,T> operator*(const Matrix<N, P, T>& B) const;
template <typename T2> operator T2() const;
private:
T data[M][N];
};
// ... the body is in header file too ...//
Run Code Online (Sandbox Code Playgroud)
身体写得很好,一切都运作良好.当我定义两个矩阵时如下:
Matrix < 10, 10, int> m1;
Matrix < 10, 10, float> m2;
m1 + m2; // OK
m1 * m2; // error: no match for 'operator*' in …Run Code Online (Sandbox Code Playgroud) 以下代码是否安全?
class B {
public:
int& b;
B (int& _b) :
b(_b) {}
};
B* foo() {
int a;
return new B(a);
}
Run Code Online (Sandbox Code Playgroud)
foo返回的对象中的引用是否会指向任何内容(因为int a超出范围)或编译是否计算出来?
看看我教授给我的一些代码,我不明白发生了什么.我是编程新手,完全迷失了.
vector <_Account*>*myvector = nullptr;
Run Code Online (Sandbox Code Playgroud)
所以我知道他创建了一个向量,我知道一个现有的类,Account所以这是一个vector指向Account对象的指针吗?我不知道第二个星号是做什么的?
我一直在使用C ++ STL创建代码。我想使用“队列”。因此,我编写了如下代码。但是,我遇到了“队列不是模板”错误。如您所见,我在“ Common.h”文件中编写了与队列(iostream,queue)相关的标头,并在“ DataQueue.h”文件中包含了“ Common.h”。但是,VS2013 IDE工具说“ queue m_deQueue”是错误的,因为queue不是模板。我不知道为什么..发生此错误。任何帮助表示赞赏!
//[Common.h]
#ifndef _COMMON_
#define _COMMON_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
//thread related headers
#include <Windows.h>
#include <process.h>
//socket related headers
#include <winsock.h>
#include <iostream>
#include <queue>
#include <deque>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
#endif
//[DataQueue.h]
#ifndef _QUEUE_
#define _QUEUE_
#include "SocketStruct.h"
#include "Common.h"
class CDataQueue{
private:
static CDataQueue* m_cQueue;
// deque <ST_MONITORING_RESULT> m_deQueue;
queue <ST_MONITORING_RESULT> m_deQueue;
CRITICAL_SECTION m_stCriticalSection;
CDataQueue();
~CDataQueue();
public:
static CDataQueue* getDataQueue(){ …Run Code Online (Sandbox Code Playgroud)