小编mch*_*mch的帖子

运算符~c编程语言

~操作员如何在c中工作?

任何人都可以解释以下代码?

main()
{
   printf("%d",~5);
}
Run Code Online (Sandbox Code Playgroud)

输出是 -6

c operators

3
推荐指数
1
解决办法
138
查看次数

在多级继承中调用析构函数(c ++)

我编写了以下类来测试多级继承概念.当我试图测试对构造函数和析构函数的调用时,有一点我真的不明白.


#include <iostream>

using namespace std;

class X{

    public:
        X(){cout <<"Construct X " << endl;};
        virtual ~X(){cout <<"Destruct X " << endl;};
        virtual void print() const = 0;
};

class Y: public X{
    public:
        Y(){cout <<"construct Y " << endl;};
        ~Y(){cout <<"Destruct Y " << endl;};
        void print() const{
            cout <<"print Y" << endl;
        };
};

class Z: public Y{
    public:
        Z(){cout <<"Construct Z" << endl; };
        ~Z(){cout <<"Destruct Z " << endl; };
        void print() const{
            cout <<" Print …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism constructor destructor

3
推荐指数
1
解决办法
345
查看次数

Strncmp 实现

我有以下带有测试驱动程序的 strncmp 函数实现,但是无法编译。

我也不确定逻辑是否正确。这是来自我的编译器的错误消息:

警告:控件可能会到达非空函数的结尾 [-Wreturn-type]

#include <stdio.h>
#include <string.h>

#undef strncmp

int strncmp(const char *s, const char *t, size_t num)
{
    for ( ; num >0;  s++, t++, num--)
        if (*s == 0)
            return 0;

    if (*s == *t) {
        ++s;
        ++t;
    }
    else if (*s != *t)
        return *s - *t;  
}

 int main ()
 {
   char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
   int n;
   puts ("Looking for R2 astromech droids...");
   for (n=0 ; n<3 …
Run Code Online (Sandbox Code Playgroud)

c string compare

2
推荐指数
1
解决办法
1万
查看次数

当前类的成员函数上的指针数组

我尝试创建一个当前类的成员函数指针数组,但暂时没有成功...

我已经尝试了很多东西,这是我的代码:

// Human.hpp

#include <iostream>

class Human
{
    private:
        void meleeAttack(std::string const & target);
        void rangedAttack(std::string const & target);
        void intimidatingShout(std::string const & target);
    public:
        void action(std::string const & action_name, std::string const & target);
};
Run Code Online (Sandbox Code Playgroud)
// Human.cpp

#include "Human.hpp"

// ...

void    Human::action(std::string const & action_name, std::string const & target)
{
    std::string actionsStr[] = {"meleeAttack", "rangedAttack", "intimidatingShout"};

    typedef void (Human::*Actions)(std::string const & target);
    Actions actions[3] = {&Human::meleeAttack, &Human::rangedAttack, &Human::intimidatingShout};

    for (int i = 2; i >= 0; i--)
        if …
Run Code Online (Sandbox Code Playgroud)

c++ arrays member-function-pointers function-pointers

2
推荐指数
1
解决办法
1638
查看次数

为什么双重类型返回Infinity?

我在C++中创建此代码只是为了检查最大的整数int数据类型可以存储.

#include<iostream>
using namespace std;
int main() {
    int a, b;
    cin >> a;
    b = a;
    for(int c; a > 1; a = a * b)
        cout << a << "\n";
};
Run Code Online (Sandbox Code Playgroud)

当我在下面的代码中输入2时,打印的最大整数是1073741824.

我将代码更改为:

#include<iostream>

using namespace std;

double main() {
    double a, b;
    cin >> a;
    b = a;
    for(double c; a > 1; a = a * b)
        cout << a << "\n";
};
Run Code Online (Sandbox Code Playgroud)

第二个代码的输出从2快速打印到无限远.为什么代码不会停止在double的最大值?为什么它会在之前的代码中停止?

c++

2
推荐指数
1
解决办法
534
查看次数

yocto 项目中的 do_rootfs 函数失败

我刚刚开始使用 yocto 项目,并尝试为 x86 架构构建一个映像,以便使用 QEMU 模拟器(在 Ubuntu 16.04 上运行)进行模拟。在构建操作系统映像时,我收到以下错误。

ERROR: core-image-sato-1.0-r0 do_rootfs: Error executing a python function in exec_python_func() autogenerated:

The stack trace of python calls that resulted in this exception/failure was:
File: 'exec_python_func() autogenerated', lineno: 2, function: <module>
     0001:
 *** 0002:do_rootfs(d)
     0003:
File: '/home/rahul/poky/poky/meta/classes/image.bbclass', lineno: 258, function: do_rootfs
     0254:    progress_reporter.next_stage()
     0255:
     0256:    # generate rootfs
     0257:    d.setVarFlag('REPRODUCIBLE_TIMESTAMP_ROOTFS', 'export', '1')
 *** 0258:    create_rootfs(d, progress_reporter=progress_reporter, logcatcher=logcatcher)
     0259:
     0260:    progress_reporter.finish()
     0261:}
     0262:do_rootfs[dirs] = "${TOPDIR}"
File: '/home/rahul/poky/poky/meta/lib/oe/rootfs.py', lineno: 1010, function: create_rootfs
     1006: …
Run Code Online (Sandbox Code Playgroud)

c linux embedded-linux yocto ubuntu-16.04

2
推荐指数
1
解决办法
6975
查看次数

C++与operator <<不匹配

这是我的代码,我不确定为什么抛出错误 - 该方法应该在指针对象上操作并打印出它的值.

主要:

cout<<"Deleted item is: "<<displayRecord(tmp)/*tmp->entry*/<<endl;


void displayRecord(PRecord* pr) {
cout<<"Time: "<<pr->time<<"\tEntry data: \""<<pr->entry<<'"'<<endl;
}
Run Code Online (Sandbox Code Playgroud)

header.h:

#include <iostream>
using namespace std;
struct PRecord {
long time;
string entry;
struct PRecord *link;
};

void displayRecord(PRecord* pr);
Run Code Online (Sandbox Code Playgroud)

我收到此错误: error: no match for 'operator <<

 error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"Deleted item is: ")) << Priority_Queue::displayRecord(tmp)'
    /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

2
推荐指数
1
解决办法
100
查看次数

静态结构警告空声明中无用的存储类说明符

  static struct astr {
          int a;
  };

  static const struct astr newastr = {
          .a = 9,
  };
Run Code Online (Sandbox Code Playgroud)

我得到:警告:空声明中无用的存储类说明符

如果我把它改成

  static struct astr {
          int a;
  } something;
Run Code Online (Sandbox Code Playgroud)

那么警告将被修复。

以下也没有给出该警告

  struct astr {
          int a;
  };

  static const struct astr newastr = {
          .a = 9,
  }; 
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下这是怎么回事吗?

c static struct

2
推荐指数
1
解决办法
937
查看次数

为什么不在 switch 语句中处理字符?C++

我试图通过使用 char +、-、/、* 来获取和处理用户决定,为什么 switch 语句忽略它们,因此我在我的代码中看不到任何错误。

#include <iostream>
using namespace std;

void optionMenu();
double UserOutput(int);

int main ()
{
    int UserChoice;

    optionMenu();
    cout << " Choice: ";
    cin >> UserChoice;
    UserOutput(UserChoice);


    return 0;
}
void optionMenu()
{
    cout << " Select your choice" << '\n';
    cout << " + for Addition" << '\n';
    cout << " - for Subtraction" << '\n';
    cout << " / for Division" << '\n';
    cout << " * for Multiplication" << '\n';
}
double UserOutput …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
107
查看次数

如何处理子函数中的const传播

我需要修改现有函数,有一些const输入参数:

int f(const owntype *r1, const owntype *r2)
Run Code Online (Sandbox Code Playgroud)

为了做到这一点,我想调用一个使用相同类型但没有const关键字的子函数:

void subfunction (owntype *src, owntype *dst)
Run Code Online (Sandbox Code Playgroud)

我已经尝试了这个(以及其他一些变体),但它不起作用:

int f(const owntype *r1, const owntype *r2) {
  ...
  subfunction((const owntyp*) r1);
  ...
}
Run Code Online (Sandbox Code Playgroud)

如何在不需要更改两个函数的parmeter描述的情况下编译它?

c const

1
推荐指数
1
解决办法
72
查看次数