小编Gha*_*ani的帖子

尽管我提供了参数,为什么我在这里得到垃圾值?

#include <iostream>
#include <math.h>
#include <conio.h>
using namespace std;

class Triangle
{
public:
    int a, b, c;
    void getdata();
    int peri(int a, int b, int c)
    {
        return a + b + c;
    }
    float s;
    float area;
    float Area(int a, int b, int c)
    {
        s = (a + b + c) / 2;
        area = sqrt(s * (s - a) * (s - b) * (s - c));
        cout << area;
    }

};

void Triangle::getdata()
{
    cin >> …
Run Code Online (Sandbox Code Playgroud)

c++ class object area

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

未定义的行为是否使用无效的迭代器?

考虑这段代码:

#include <iostream>
#include <string>
#include <map>

int  main()
{
  std::map<std::string, std::string>  map = {
    { "ghasem", "another" }
  };
  std::cout << map.find("another")->second << std::endl;
  std::cout << map.size() << std::endl;
}

Run Code Online (Sandbox Code Playgroud)

它将被编译并成功运行(进程返回值为0),但我们看不到 的输出map.size()。既-fsanitize=address没有-fsanitize=undfined报告任何问题。我用GCC-11.2.1和Clang-13.0.0编译,两者是一样的。使用 GDB-11.1-5 逐步运行代码不会有帮助,所有步骤都会成功运行。

但如果我重新排序最后两行:

#include <iostream>
#include <string>
#include <map>

int  main()
{
  std::map<std::string, std::string>  map = {
    { "ghasem", "another" }
  };
  std::cout << map.size() << std::endl;
  std::cout << map.find("another")->second << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我将收到分段错误,现在 ASAN 可以报告该错误。


我的问题是:代码是否会导致某种未定义的行为?我怎样才能检测到这些错误?


环境:
  • 操作系统 …

c++ undefined-behavior

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

让成员函数返回类型成为要返回的实际对象的超类的正确方法是什么?

问题

让成员函数返回类型成为您要返回的实际对象的超类的正确方法是什么?

问题

Clang tidy 警告我返回subClassAsubClassB丢弃我对myMethod. 我也没有得到任何输出 - 大概是因为我不小心丢弃了覆盖的myMethod.

编码

#include <iostream>

using namespace std;

class SuperClass {
public :
    SuperClass() {
        cout << "I'm the superclass" << endl;
    };

    virtual std::string myMethod();
};

class SubClassA : public SuperClass {
public:
    SubClassA() : SuperClass() {
        cout << "I'm subclass A" << endl;
    }

    std::string myMethod() override {
        return "A";
    }
};

class SubClassB : public SuperClass {
public:
    SubClassB() : SuperClass() { …
Run Code Online (Sandbox Code Playgroud)

c++ oop polymorphism

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

如何从函数返回指向 char[][][] 的指针?

我有一个static-char 数组定义为:

static const char city_names[1000][4][50];
Run Code Online (Sandbox Code Playgroud)

我想从函数返回一个指向这个变量的指针,我尝试 a static_casttovoid*但它失败了。我怎样才能返回一个指针char[][][]

c++ arrays pointers declaration function-declaration

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

C++ 中嵌套 If 的行为是什么?

#include <iostream>
using namespace std;
int main()
{
    int a = 1;
    int b = 2;
    if(a > 0)
    {
        cout << "we are in the first" << endl;
        if(b > 3)
        {
            cout << "we are in the second" << endl;
        }
    }
    else
    {
        cout << "we are in else" << endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

根据 C++ISO:

在 if 语句的第二种形式(包括 else 的形式)中,如果第一个子语句也是 if 语句,则该内部 if 语句应包含 else 部分。换句话说,else 与最近的 un-elsed if 相关联。

我认为上面的代码会打印出“we are in else”,因为最近的 un-elsed 的条件if …

c++ if-statement

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

Clang 错误地选择了 GCC 的包含目录

我使用包管理器从 apt.llvm.org 存储库安装了 LLVM+Clang apt。但我无法正确使用它。

$ clang++ -E -v -
Debian clang version 15.0.7
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64

...
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/llvm-15/lib/clang/15.0.7/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /include
 /usr/include
Run Code Online (Sandbox Code Playgroud)

它不包含 Clanglibc++标头的实际路径。考虑以下代码:

#include <type_traits>

enum A { B };
int main() { std::is_scoped_enum_v<A>; } …
Run Code Online (Sandbox Code Playgroud)

c++ linux clang

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

矩阵使用指针 C++ 没有动态分配

所以我有一个赋值可以使用指针读取矩阵并使用指针显示它我也想知道为什么它总是在错误的地方添加一行并且它不显示矩阵也thnks

#include <iostream>

using namespace std;

int main ()
{
    int Mat[50][50],N,M;
    int (*ptr)[50][50];
    cout<<"number of lignes : ";
    cin>>N;
    cout<<"number of rows : : ";
    cin>>M;
    ptr=&Mat;
    
    for (int i=0;i<N;i++)
     {
        for (int j=0 ; j<M ;j++)
         {
              cout<<"fill the matrix [" <<i<<","<<j<< "] : " ;
              cin>>*ptr[i][j];  
         }
     }
    
    for (int i=0 ; i<N ; i++ )
     {
        for (int j=0 ; j<M ; j++)
        {
            cout<<*ptr[i][j] <<" ";
        }
        cout<"/n";
     }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

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

是否可以确保始终首先评估 &amp;&amp; 运算符的左侧?

正如标题所说:C++ 标准是否有任何保证可以确保&&(或and) 运算符的左侧始终首先计算?老实说,我在C++17 Standard 中搜索不到,我不知道我最想找哪个部分。

问题示例:

我想做这样的事情:

std::unordered_map<std::size_t, std::weak_ptr<T>> objects;

bool f (std::size_t const id) {
  bool result = false;

  if (not objects.at(id).expired()) {
    auto const& object = objects.at(id).lock();

    /* Here left side of `and` most be evaluated first */
    result = object->parent->remove(id) and 
             objects.erase(id) == 1;
  }

  return result;
}
Run Code Online (Sandbox Code Playgroud)

并且要确保代码没有问题。

c++ language-lawyer

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