我正在尝试为一个函数设置一个委托,并有两个以下的类来实现它.
在底部是我得到的错误.我该如何处理?
A级
typedef void (*SocketEventString) (String);
class SocketIO 
{
    public:
        SocketIO();
        void onMessage(SocketEventString _cb);
    private:
        SocketEventString _onMessage;
};
B级
class BoardManager 
    {
        public:
            BoardManager();
            void handleAction(String action);
            SocketIO io;
    };
//Constructor
BoardManager::BoardManager() {
    io.onMessage(  std::bind( &BoardManager::handleAction, this, std::placeholders::_1 ) );
}
错误
sketch/BoardManager.cpp: In member function 'void BoardManager::initializeSocketIO()':
BoardManager.cpp:68: error: no matching function for call to 'SocketIO::onMessage(std::_Bind_helper<false, void (BoardManager::*)(String), BoardManager* const, const std::_Placeholder<1>&>::type)'
     io.onMessage(  std::bind( &BoardManager::handleAction, this, std::placeholders::_1 ) );
                                                                                          ^
sketch/BoardManager.cpp:68:90: note: candidate is:
In file included from …我有一门功课,我应该等到我写控制台“停止”一次又一次地做一些动作,但我不能使用for,while,goto,switch,[],typedef在我的所有代码。那么如何更换循环呢?
I'm just wondering why this standard function is returning a char count of 9 for the sample code at cplusplus.com
// cin.gcount example
#include <iostream>     // std::cin, std::cout
int main () {
  char str[20];
  std::cout << "Please, enter a word: ";
  std::cin.getline(str,20);
  std::cout << std::cin.gcount() << " characters read: " << str << '\n';
  return 0;
}
Please, enter a word: simplify
读取9个字符:简化
为什么返回9个字符?
为什么这个代码不给预期值的n = 1000000000000预期value = 1099511627775
虽然代码是给255
long long now = n, count = 0, len = 0;
while (now >= 1) {
  count++;
  now /= 2;
}
len = (1 << count) - 1;
cout << len;
#include <stdio.h>
int
main()
{
  int i;
  int c;
  int a[30] = { 5,  7,  11, 13,  17,  19,  23,  29,  31,  37,
                41, 43, 47, 53,  59,  61,  67,  71,  73,  79,
                83, 89, 97, 101, 103, 107, 109, 113, 127, 131 };
  for (i = 0; i < 30; i++) {
    c = (a[i] + i) / (i - 1);
    printf("Value of c is %d", c);
  }
}
我不明白为什么我在这个程序中面临浮点异常错误。
我面临的问题与记录器库有关。我可以创建自己的记录器,但是同时,我希望能够使用“默认”或“全局”记录器进行操作。
因此,我认为记录器类的某些方法应具有静态版本,这些静态版本将处理此“默认”记录器。最终在设计方面感觉不对。
这是我想做的一个例子
std::shared_ptr<lwlog::logger> core_logger = std::make_shared<lwlog::logger>("LOGGER");  //creating some custom logger
core_logger->critical("A very critical message!"); //logging from some custom logger
lwlog::logger::critical("A very critical message!"); //logging from default logger
我的静态和非静态版本的方法的示例:
class LWLOG logger
{
public:
    explicit logger(const std::string& name);
    ~logger();
    void set_name(const std::string& loggerName);
    void set_logLevel_visibility(log_level logLevel);
    void set_pattern(const std::string& pattern);
    void info(const std::string& message);
    void warning(const std::string& message);
    void error(const std::string& message);
    void critical(const std::string& message);
    void debug(const std::string& message);
    static void info(const std::string& message);
    static void warning(const std::string& message); …如果两个整数x和y的按位与的结果等于0,则它们构成一个魔幻对。给定一个整数数组,请为每个数组元素查找是否与其他数组元素形成了魔幻对。
输入项
输入的第一行包含一个整数T,表示测试用例的数量。每个测试用例的第一行都有一个整数N,表示给定数组中元素的数量。第二行包含N个单个以空格分隔的整数a1,a2,...,表示给定数组的元素。
输出量
对于每个测试用例,在一行中打印N个空格分隔的整数。如果ai与给定数组的任何其他元素形成一个魔幻对,则ans'i应该等于1。否则ans'i为0。
约束条件
1 <= N,Ai <= 10 ^ 6
我尝试过蛮力。对于每个元素,我检查此数字的按位与是否为零,或者是否与数组中存在的任何其他元素相同。显然,它的时间复杂度为O(N ^ 2),我的大多数测试用例都超时了
这个问题在这里:https : //www.hackerearth.com/challenges/test/netapp-codenet-2017/algorithm/d2d1f6a92c6740278682e88ed42068a4/
谁能建议我一个更好的方法或算法,使其超过时间限制?
蛮力代码:
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
    cin >> a[i];
int ans[n];
for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
        if (a[i] & a[j] == 0)
            ans[i] = 1;
for (int i = 0; i < n; i++)
    cout …我试图创建一个XOR运算符,但由于未知原因,我的编译器不接受bool xor()该函数,它也不允许我调用它或以任何可能的方式使用它。
我想指出的是,我正在学习一本书来学习C ++。具体地说,它是Herbert Schildt(第3版)的“从头开始学习C ++”。本书中引用了这段代码。
如果我将函数命名为bool xar()或bool XOR(),我的代码就可以正常工作,但是由于我正在尝试学习C ++,所以我想对为什么会发生此错误有所了解。
#include <iostream>
using namespace std;
bool xor(bool a, bool b);
int main()
{
    bool q, p;
    cout << "Enter Q (0 or 1): ";
    cin >> q;
    cout << "Enter P (0 or 1): ";
    cin >> p;
    cout << "Q AND P: " << (q && p) << '\n';
    cout << "Q OR P: " << (q || p) << '\n';
    cout << …下面的函数原型声明之间有什么区别?
int a(double = 0, double = 10);
int a(double j = 0, double a = 10);
我正在尝试更改向量的每个元素,但是由于对consts 的某些混淆,我的代码无法编译。这是一个大大简化的示例(我省略了向量的初始化):
std::vector<int> test;
for (auto iter = test.cbegin(); iter != test.cend(); ++iter)
{
    int v = *iter; // gets the correct element
    *iter = v * 2; // compile error
}
编译器错误为“'iter':您无法分配给const变量”。如图所示,使用迭代器修改单个元素的正确方法是什么?