我不太明白为什么我不能除以零例外:
int d = 0;
d /= d;
Run Code Online (Sandbox Code Playgroud)
我本来希望得到除以零的除法运算,但是反而d == 1
。
为什么在什么时候不d /= d
将被零除d == 0
?
c++ division divide-by-zero compiler-optimization undefined-behavior
C++20 引入了显式 (bool),它在编译时有条件地选择构造函数是否显式。
下面是我在这里找到的一个例子。
struct foo {
// Specify non-integral types (strings, floats, etc.) require explicit construction.
template <typename T>
explicit(!std::is_integral_v<T>) foo(T) {}
};
foo a = 123; // OK
foo b = "123"; // ERROR: explicit constructor is not a candidate (explicit specifier evaluates to true)
foo c {"123"}; // OK
Run Code Online (Sandbox Code Playgroud)
谁能告诉我explicit (bool)
除了 using 之外的任何其他用例std::is_integral
?
第一次使用堆栈溢出。我正在尝试实践面向对象的抽象和接口。我一直无法编译程序,我的程序如下。
主程序
#include "Spells.h"
#include <iostream>
int main()
{
spell MagicalSpell;
std::cout << "Hello World!\n";
}
Run Code Online (Sandbox Code Playgroud)
Spells.h
#pragma once
class spell {
private:
int EnergyCost;
public:
spell();
~spell();
void SpellEffect();
};
Run Code Online (Sandbox Code Playgroud)
拼写.cpp
#include "Spells.h"
#include <iostream>
spell::spell() {
EnergyCost = 0;
}
spell::~spell() {
}
void spell::SpellEffect(){
std::cout << "woosh" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
每次我尝试编译 main.cpp 时,我都会得到:
g++ main.cpp -lcrypt -lcs50 -lm -o main
/tmp/ccnXk1TN.o: In function `main':
main.cpp:(.text+0x20): undefined reference to `spell::spell()'
main.cpp:(.text+0x3f): undefined reference to `spell::~spell()'
main.cpp:(.text+0x64): undefined reference …
Run Code Online (Sandbox Code Playgroud)