我有以下类,该类有一个名为的方法errorHandler,需要使用多个不同的回调:
class IOPin;
class IOPinHandler
{
IOPinHandler();
virtual ~IOPinHandler();
static bool ptrFun(IOPinHandler&) { return true; };
template<typename T>
bool init(IOPin& obj, const T& param);
template<typename HandlerReturn = void, typename ...Args>
HandlerReturn errorHandler(const GpioStatusCode& code, HandlerReturn(*callback)(IOPinHandler& obj, const Args&...), const Args&... args);
// Added this overload to support passing lambdas as arguments,
// however does not seems to be called
template<typename HandlerReturn = void, typename ...Args>
HandlerReturn errorHandler(const GpioStatusCode& code, const std::function<HandlerReturn(IOPinHandler&, const Args&...)>& callback, Args&... args);
};
Run Code Online (Sandbox Code Playgroud)
我可以使用以下内容: …
考虑以下场景:
template <
typename T,
bool B = std::is_default_constructible_v<T>>
class toy_example;
template<typename T>
class toy_example<T, true>
{
public:
toy_example() = default; // e.g. for default constructible types
toy_example(const T& value);
public:
void monomorphic(T);
private:
T m_value;
};
template<typename T>
class toy_example<T, false>
{
public:
toy_example() = delete; // e.g. for non-default constructible types
toy_example(const T& value); // Repeated declaration
public:
void monomorphic(T); // Repeated declaration
private:
T m_value;
};
// Implementation
template<typename T>
toy_example<T, true>::toy_example(const T& value) : m_value(value) {} …Run Code Online (Sandbox Code Playgroud) 我最近改变了我的原始指针,std::shared_ptr在这样的容器中使用:
std::vector<std::shared_ptr<AbstractPathContainer>> mGeneratedPaths;
Run Code Online (Sandbox Code Playgroud)
当我打电话clear()给这个容器时,它会reset()在每个容器上调用方法std::shared_ptr吗?
我确信有一种简单的方法可以做到这一点,但在 SO 中找不到任何东西。在en.cppreference.com 中也找不到太多信息。
有没有办法简化 ,std::variant</*class types*/>以便我们可以声明可以std::variant作为参数的函数和类。
考虑这个例子:
我有一个向量,它充当以下内容的容器std::variant;
std::vector<std::variant<Car, Space, Human>> EntityContainer;
Run Code Online (Sandbox Code Playgroud)
如果我想将此向量作为参数传递给函数,则必须添加以下参数声明。
void Function(std::vector <std::variant<Car, Space, Human>>& container);
Run Code Online (Sandbox Code Playgroud)
我也许可以在这个例子中使用宏,但这并不能真正解决问题。
有没有更好的解决方案,而不是std::variant在项目的任何地方一遍又一遍地列出相同的类类型?
#include <iostream>
#include <vector>
#include <variant>
class Human
{
public:
void Talk(){ std::cout << "whass up\n"; }
};
class Car
{
public:
void RunEngine() { std::cout << "Vroom\n"; }
};
class Space
{
public:
void Expand() { std::cout << "Expand slowly\n"; }
};
struct VisitPackage …Run Code Online (Sandbox Code Playgroud) 当我尝试创建此类的实例时,我遇到此代码的问题,似乎出现错误
not allowed to use the abstracted class "SavingAccount"
Run Code Online (Sandbox Code Playgroud)
我不知道我能做什么.我按照edx Microsoft Intermediate C++给出的步骤进行操作.
BankAccount.h
#pragma once
#include <string>
class BankAccount
{
protected:
double balance;
public:
BankAccount(double initialBanlance);
virtual ~BankAccount();
double getBalance() const;
virtual void deposit(double amount);
virtual void withdraw(double amount);
virtual std::string getTermAndConditions() = 0;
virtual double getGuaranteeLimit() = 0;};
Run Code Online (Sandbox Code Playgroud)
BankAccount.cpp
#include "BankAccount.h"
BankAccount::BankAccount(double initialBanlance)
:balance(initialBanlance)
{}
BankAccount::~BankAccount()
{}
double BankAccount::getBalance() const
{return balance;}
void BankAccount::deposit(double amount)
{
balance += amount;
}
void BankAccount::withdraw(double amount)
{balance -= amount;}
Run Code Online (Sandbox Code Playgroud)
Freezable.h
#pragma …Run Code Online (Sandbox Code Playgroud) 我在一个问题中使用,但到目前为止dfs我还没有调用主函数,我的程序崩溃了。dfs最近我在编程c,现在我切换到cpp. 所以我是新手cpp。
我知道我在向量中哪里出错了,请告诉我可以改进什么。我知道矢量可以自动增加其大小。
#include<iostream>
#include<vector>
using namespace std;
const int MAX = 100000;
bool visited[MAX] = { 0 };
int intime[MAX];
int outtime[MAX];
int timer = 0;
void dfs(vector<vector<int>> graph, int v)
{
visited[v] = true;
timer++;
intime[v] = timer;
vector<int>::iterator it = graph[v].begin();
while (it != graph[v].end()) {
if (visited[*it] == false)
{
dfs(graph, *it);
}
it++;
}
++timer;
outtime[v] = timer;
}
int main()
{
vector<vector<int>> graph;
graph[1].push_back(2); …Run Code Online (Sandbox Code Playgroud) enum class旨在提供类型安全性,从而通过整数提升消除了隐式转换为整数的方法,但是一旦获得std::underlying_type_t优势,enum class我就不清楚。
假设我有一个整数数组表示棋盘上的棋子;
int board[8][8];
Run Code Online (Sandbox Code Playgroud)
在我的国际象棋游戏中,我目前正在编写一个生成器函数,它将返回所有合法移动的整数向量。
当然,我将使用if 语句
我现在需要检查棋盘上的某个元素相对于棋盘上的一块
例如,如果我有一个棋子;
board[row][col] == 'p';
Run Code Online (Sandbox Code Playgroud)
我需要生成[row+1][col],[row+2][col]并且在某些情况下,如果它可以攻击一块,那么列中也会发生变化。
但是如果一块位于 的任何边缘board,board[row+1][col]将返回索引超出范围
出于这个原因,我需要一个额外的 if 语句。
我的问题是,我应该使用:
if (pieceisnotonedge && board[row+1][col] == 0)
Run Code Online (Sandbox Code Playgroud)
或者
if (pieceisnotonedge)
{
if (board[row+1][col] == 0)
}
Run Code Online (Sandbox Code Playgroud)
对于第一个示例,如果pieceisnotonedge返回false,它还会检查下一个条件吗?因为如果是这样,那我就有麻烦了。
我在 main.cpp 中有一个例程,用户将在其中指定要在程序中执行的模式。一旦指定了模式,就会执行相应的块——首先将父求解器向下转换为子求解器,并调用solve子类中的关联方法。
std::unique_ptr<SudokuSolver> solver;
if (mode == MODES::SEQUENTIAL_BACKTRACKING)
{
solver = std::make_unique<SudokuSolver_SequentialBacktracking>();
SudokuSolver_SequentialBacktracking* child_solver = dynamic_cast<SudokuSolver_SequentialBacktracking*>(solver.get());
child_solver->solve(board);
}
else if (mode == MODES::SEQUENTIAL_BRUTEFORCE)
{
solver = std::make_unique<SudokuSolver_SequentialBruteForce>();
SudokuSolver_SequentialBruteForce* child_solver = dynamic_cast<SudokuSolver_SequentialBruteForce*>(solver.get());
child_solver->solve(board);
}
else if (mode == MODES::PARALLEL_BRUTEFORCE)
{
int NUM_THREADS = (argc >= 5) ? std::stoi(argv[4]) : 2;
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel
{
#pragma omp single nowait
{
solver = std::make_unique<SudokuSolver_ParallelBruteForce>();
SudokuSolver_ParallelBruteForce* child_solver = dynamic_cast<SudokuSolver_ParallelBruteForce*>(solver.get());
child_solver->solve(board);
}
}
}
else if (mode == MODES::SEQUENTIAL_DANCINGLINKS)
{
solver …Run Code Online (Sandbox Code Playgroud) 代码
#include <iostream>
class A
{
public:
mutable int x;
mutable int y;
A(int k1 = 0, int k2 = 0) :x(k1), y(k2) {}
void display()
{
std::cout << x << "," << y << "\n";
}
};
int main()
{
const A a1;
a1.x = 3;
a1.y = 8;
a1.display();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
Error: 'this' argument to member function 'display'
has type 'const A', but function is not marked const
Run Code Online (Sandbox Code Playgroud)
我只是A::display()通过const限定对象调用成员函数a1。那么为什么 line …
c++ ×10
c++11 ×3
c++17 ×3
templates ×3
algorithm ×2
class ×2
stdvector ×2
c++14 ×1
constants ×1
enums ×1
if-statement ×1
inheritance ×1
logic ×1
mutable ×1
reset ×1
sfinae ×1
shared-ptr ×1
std-variant ×1
variant ×1