#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) 考虑这段代码:
#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 可以报告该错误。
我的问题是:代码是否会导致某种未定义的行为?我怎样才能检测到这些错误?
让成员函数返回类型成为您要返回的实际对象的超类的正确方法是什么?
Clang tidy 警告我返回subClassA或subClassB丢弃我对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) 我有一个static-char 数组定义为:
static const char city_names[1000][4][50];
Run Code Online (Sandbox Code Playgroud)
我想从函数返回一个指向这个变量的指针,我尝试 a static_casttovoid*但它失败了。我怎样才能返回一个指针char[][][]?
#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 …
我使用包管理器从 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) 所以我有一个赋值可以使用指针读取矩阵并使用指针显示它我也想知道为什么它总是在错误的地方添加一行并且它不显示矩阵也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++ 标准是否有任何保证可以确保&&(或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++ ×8
area ×1
arrays ×1
clang ×1
class ×1
declaration ×1
if-statement ×1
linux ×1
object ×1
oop ×1
pointers ×1
polymorphism ×1