我正在围绕std :: set创建一个模板包装器.为什么我的Begin()函数声明会出错?
template <class T>
class CSafeSet
{
public:
CSafeSet();
~CSafeSet();
std::set<T>::iterator Begin();
private:
std::set<T> _Set;
};
Run Code Online (Sandbox Code Playgroud)
错误:类型'std :: set,std :: allocator <_CharT >>'不是从'CSafeSet'类型派生的
我在2个不同的文件中有2个类:
RegMatrix.h:
#ifndef _RM_H
#define _RM_H
#include "SparseMatrix.h"
...
class RegMatrix{
...
RegMatrix(const SparseMatrix &s){...} //ctor
...
};
#endif
Run Code Online (Sandbox Code Playgroud)
SparseMatrix.h:
#ifndef _SM_H
#define _SM_H
#include "RegMatrix.h"
...
class SparseMatrix{
...
SparseMatrix(const RegMatrix &r){...} //ctor
...
};
#endif
Run Code Online (Sandbox Code Playgroud)
在构造函数行上我得到错误:
错误C4430:缺少类型说明符 - 假定为int.
错误C2143:语法错误:'&'之前缺少','
但是当我添加类声明时
class SparseMatrix;
Run Code Online (Sandbox Code Playgroud)
在RegMatrix.h文件中
class RegMatrix;
Run Code Online (Sandbox Code Playgroud)
在SparseMatrix.h文件中它工作正常.我的问题是,如果我有包含,为什么需要它?10X.
我认为"const-correctness"的概念定义得很明确,但当我与其他人谈论它时,似乎我们对它的含义有不同的看法.有人说它是关于在尽可能多的地方有"const"注释的程序.其他人将程序定义为const-correct,当且仅当使用const注释时没有违反constness(即,它是编译器为您检查的属性).
所以我想知道,这些函数中哪些是正确的:
struct Person {
string getName1() const { return _name; }
string getName2() { return _name; }
string getName3() const { _name = "John"; return _name; }
string getName4() { _name = "John"; return _name; }
string _name;
};
Run Code Online (Sandbox Code Playgroud)
我在互联网上搜索了定义,但实际上找不到明确的答案,我也怀疑可能存在一个发生过中的柑橘生成案例.那么任何人都可以为定义提供任何可靠的引用吗?
我有一个需要在Windows,Linux和VxWorks上构建的项目.该项目建立在Linux和Windows上,但是为VxWorks交叉编译.要处理跨多个平台的字节序,它使用ntoh.h. Linux机器是小端,但是ntohl不会交换我的程序.
我写了一个直接包含in.h的测试程序.交换得恰到好处.我写了另一个包含ntoh.h的测试程序.交换得恰到好处.两个测试程序都链接到lib64/libc.so.6.
但是,当我编译我的项目时,ntohl不会交换.我无法使用gdb"break ntohl"命令打破ntohl.在构建时,我看到LITTLE ENDIAN警告(见下文)并且没有看到"SHOULDNT BE HERE"错误.
请帮忙.我不明白为什么会出现这个问题.
下面是ntoh.h:
#ifndef __ntoh__
#define __ntoh__
#include "basic_types.h"
#ifdef WIN32
#include <winsock2.h>
#elif LINUX
#include <netinet/in.h>
//This is here to determine what __BYTE_ORDER is set to in netinet/in.h.
// Not in original code
#if __BYTE_ORDER == __BIG_ENDIAN
#warning BIG ENDIAN BYTE ORDER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#endif
//This is here to determine what __BYTE_ORDER is set to in netinet/in.h.
// Not in original code
#if __BYTE_ORDER == __LITTLE_ENDIAN
#warning YAY LITTLE ENDIAN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! …Run Code Online (Sandbox Code Playgroud) 我目前正在练习用C++编写类和头文件.我有一个问题:让我们说在我的头文件中我有一个客户端可以使用的公共函数,我知道如何在相应的类中实现它.但是,让我们说这个功能分为几个步骤,可以写成我不希望用户看到的独立功能(保护知识产权).通常,对于头文件中的每个已定义函数,我都会在.cpp文件中的myClassName :: myFunctionName(参数1 ..)中编写.有没有办法只在.cpp文件中定义和使用函数?例如,我写了一个程序来查看两个单词是否是字谜(具有相同的字母).
我的头文件是:
#ifndef _Anagrams_h
#define _Anagrams_h
#include <string>
using namespace std;
class Anagrams{
public:
Anagrams(string &s);
static bool areTwoWordsAnagrams(string s1, string s2) ;
string getWord()const;
void setWord(string &s);
private:
string word;
};
#endif
Run Code Online (Sandbox Code Playgroud)
我的班级是:
#include "Anagrams.h"
#include <string>
using namespace std;
Anagrams::Anagrams(string &s){
word = s;
}
bool Anagrams::areTwoWordsAnagrams(string word1, string word2){
int sizeOfWord1 = word1.size();
int sizeOfWord2 = word2.size();
int array1[26];
int array2[26];
for (int i = 0; i < 26; i++){ //Initialize both arrays
array1[i] …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个流运算符<<,它可以输出到一个std::tuple流而不是一个流.所以基本上,我试图tee用c ++ 编写Unix 命令,并且:
std::tie(std::cout,std::clog) << 1;
Run Code Online (Sandbox Code Playgroud)
我试图在c ++ 11中使用可变参数模板编程递归地编写流操作符.到目前为止我所拥有的内容如下面的代码所示.但是代码没有编译,错误信息很长.
我的问题是,如何修复代码使其工作?
编译g++ -std=c++11(gcc-4.8.1)的第一条错误消息是:
test.cpp:24:33: error: no match for 'operator<<' (operand types are 'std::tuple<std::basic_ostream<char, std::char_traits<char> >&, std::basic_ostream<char, std::char_traits<char> >&>' and 'int')
std::tie(std::cout,std::cout) << 1;
Run Code Online (Sandbox Code Playgroud)
PS我搜索了SO,并且有一些关于编写复合流对象的帖子.该代码涉及很多流和streambuf的内部.我在这里寻求的是一个简单/天真的解决方案,在十几行中完成类似的任务.
谢谢
我的代码:
#include <iostream>
#include <tuple>
template<typename _Value, typename Head, typename ... Tail>
struct _tee_stream {
static std::tuple<Head,Tail...>& _print(std::tuple<Head,Tail...>& _s, const _Value& _t) {
return std::make_tuple(std::get<0>(_s) << _t ,_tee_stream<Tail...,_Value>::_print(_s,_t));
}
};
template<typename _Value>
struct _tee_stream<_Value, std::tuple<>> {
static …Run Code Online (Sandbox Code Playgroud) 我正在 C++11 中实现多索引映射,我希望针对特定功能对其进行优化。我目前正在尝试解决的问题是不要多次存储关键元素。但让我解释一下。
问题来自对直方图进行排序以将它们以不同的组合覆盖。直方图有名称,可以拆分为标记(属性)。
以下是我希望我的房产地图具有的功能:
我有一个工作实现在C ++ 11使用std::unordered_map与std::tuple作为key_type。当属性值到达一个 forward_lists 元组时,我正在累积它们。预期用途是遍历列表以组合键。
我想介绍的优化是只将属性的值存储在列表中,而不是将它们存储在用作映射键的元组中。我想保持让函数返回对属性值列表的常量引用的能力,而不是一些包装器的列表。
我知道boost::multi_index具有类似的功能,但我不需要在键到达时进行排序的开销。我希望按顺序存储新的属性值,并且只能在事后排序。我也看过boost::flyweight,但在最简单的方法中,列表将是 offlyweight<T>而不是T,我不想这样做。(如果这是最好的解决方案,我绝对可以接受。)
我知道列表是稳定的,即一旦创建了一个元素,它的指针和迭代器仍然有效,即使在调用list::sort(). 知道这一点,可以对地图做些什么来消除元组元素的冗余副本吗?自定义地图分配器可以在这里提供帮助吗?
感谢您的建议。
我想通过包装 C++11 中的 std::thread 类来使用我自己的 Thread 实现,这样我就可以像我想要的那样处理异常。
这是我的包装类:
#include <Types.hpp>
#include <thread>
#include <exception>
#include <functional>
class Thread
{
private:
std::exception_ptr exceptionPtr;
std::thread thread;
public:
using Id = std::thread::id;
using NativeHandleType = std::thread::native_handle_type;
Thread() noexcept = default;
Thread(Thread &&t) noexcept :
exceptionPtr(std::move(t.exceptionPtr)),
thread(std::move(t.thread))
{
}
Thread &operator =(Thread &&t) noexcept
{
exceptionPtr = std::move(t.exceptionPtr);
thread = std::move(t.thread);
return *this;
}
template<typename Callable, typename... Args>
Thread(Callable &&f, Args &&... args) :
exceptionPtr(nullptr),
thread([&](Callable &&f, Args &&... args)
{
try
{
std::once_flag …Run Code Online (Sandbox Code Playgroud) 当我查看 GNU 库的实现(好吧,主要是 libstdc++)时,我可以看到命名中有重复出现的模式。模板类型被命名_Tp,成员有前缀_M_,一些标记有双下划线等。我试图找到有关命名约定的文档,但无济于事。GNU 有一个样式指南,它也在代码中遵循,但更像是这个命名约定的一个子集。
你知道关于 GNU gcc 库实现的样式细节的任何文档吗?
提前致谢。
我正在尝试对以下元素进行比较:
std::vector<std::array<uint8_t, 6> > _targets =
{
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x11 }
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x22 }
};
Run Code Online (Sandbox Code Playgroud)
传统阵列:
uint8_t _traditional[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x33 }
Run Code Online (Sandbox Code Playgroud)
如:
for (auto target : _targets)
{
if (! memcmp(target, _traditional, 6)) {
known = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
并收到数据转换错误:
error: cannot convert 'std::array<unsigned char, 6u>' to 'const void*' for argument '1' to 'int memcmp(const
void*, const void*, size_t)
Run Code Online (Sandbox Code Playgroud)
我可以执行什么属性字节比较操作来完成平等评估?