一旦我知道这个主题属于什么主题,我会很乐意将这个主题的标题更改为更合适的内容.
如果我更改了导致错误的构造函数的参数,则没有错误.
如果我包含那个确切的构造函数,则只会出现此错误:
error: no matching function for call to 'Object::Object(Object)'
note: candidates are: Object::Object(Object&)
note: Object::Object()
Run Code Online (Sandbox Code Playgroud)
码:
#include <iostream>
using namespace std;
class Object {
public:
Object() {} /* default constructor - no problems at all */
Object( Object & toCopy ) {} /* Cause of error, but no error when commented out */
Object func() {
Object obj;
return obj;
}
};
int main() {
Object o = o.func(); /* this is the line that the error is …Run Code Online (Sandbox Code Playgroud) 所以我有一个小程序
#include <iostream>
using namespace std;
void lol() {
cout << "How did we get here?"<<std::endl;
}
int main()
{
long a, b, z[10];
cin >> a >> b;
z[a] = b;
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处通过在线编译器运行它
该程序没有任何目的,但它有一个错误或功能 - 我不知道它是什么.所以,如果你写这样的东西,
main 13 2015你可能什么也得不到,但是如果你输入两个幻数13,4196608你就会得到一个错误.此外,程序执行功能void lol()并打印线How did we get here?.
我跑了nm ./main,发现我的函数void lol()的地址0000000000400900等于4196608(数字系统的基数是10).
这意味着程序由于某种原因"跳转"到该地址并执行该功能void lol().而且,如果我改变第一个数字,什么都不会发生.
main 10 4196608,main 11 4196608,main …
int main(void)
{
int n1 = 2, n2 = 5;
int *p = &n1, *q = &n2;
*p = *(q++);
printf("%d,%d", *p, *q);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出= 5,5
为什么值为*q5它应该有一些垃圾值?
int main(void)
{
int n1 = 2, n2 = 5;
int *p = &n1, *q = &n2;
*p = *(++q);
printf("%d,%d", *p, *q);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出= 2,2
这是怎么回事?任何人都可以解释优先规则如何在指针中工作?
我正在尝试用我的类和std :: vector做一些工作.我遇到了以下问题,=
这是我的代码:
class A
{
private:
........
public:
vector<int>* ret_vec();
};
vector<int>* A::ret_vec()
{
vector<int> vec; // vec is declared as a local variable
......
return &vec;
}
int main()
{
.....
vector<int>* vec = A.ret_vec(); // size of vec is 0 !!!
}
Run Code Online (Sandbox Code Playgroud)
正如您在代码中看到的那样,vecin vector<int>* A::ret_vec()被声明为局部变量,当我将其指定为指针时main,其大小vec为0.
因此,我使用以下方法:
class A
{
private:
vector<int> vec; // vec is declared as a member variable
public:
vector<int>* ret_vec();
};
vector<int>* A::ret_vec()
{
......
return …Run Code Online (Sandbox Code Playgroud) 我在C++中的void delete []有一些问题这是我的代码:
#include<iostream>
int main(){
int *p = new int[1000];
for (int i = 1; i <= 1000; ++i)
p[i] = i;
delete [] p;
for (int i = 0; i <= 1000; ++i)
std::cout << std::endl << p[i];
}
Run Code Online (Sandbox Code Playgroud)
从理论上讲,当我调用delete [] p时,必须删除指针p.但是,在调用delete [] p后,我仍然可以将它打印到屏幕上.谁能解释为什么?
我很难理解为什么这个代码会导致3f000000.
float f = 5e-1;
printf("%x", *(int*)&f);
Run Code Online (Sandbox Code Playgroud) 我在C++中创建了这样的代码:
int main() {
int myArray[5];
int n =1;
#define EVER ;;
for(EVER){
myArray[n] +=n++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我让它运行,我得到一个blueScreen.在此之后我的电脑不想启动.我做错了什么?我是C++的新手.谢谢
当使用变量num时,我在访问第9个索引时遇到堆栈损坏的错误,但是当我使用任何其他名称访问时,它会出错.请解释我这种含糊不清的地方.
#include<iostream>
#include<iomanip>
using namespace std;
const int SIZE = 9;
class A
{
private:
int num[SIZE];
int num_2[SIZE];
public:
A()
{
for(int i = 1 ; i <= 9 ; i++)
{
num_2[i] = i;
}
}
};
void main()
{
A obj;
}
Run Code Online (Sandbox Code Playgroud) 我是 C++ 新手,正在阅读《使用 C++ 的编程原理和实践》一书。我遇到了一些我无法完全理解的事情。希望有人能帮助我理解这一点。
为什么这是无效的?
int* p = nullptr;
*p = 7;
Run Code Online (Sandbox Code Playgroud) 今天我对此感到惊讶:
\n#include <stdlib.h> // for the 'exit' call\n\nint foo() {\n // return 0;\n}\n\nint main() {\n int res = foo();\n exit(res);\n}\nRun Code Online (Sandbox Code Playgroud)\n我知道忘记返回 ; 中的预期整数值不是好形式foo。但你会期望这段代码出现段错误吗?
以下是 GCC7.5 中发生的情况:
\n(thanassis)$ g++ -O3 -Wall a.cc\na.cc: In function \xe2\x80\x98int foo()\xe2\x80\x99:\na.cc:5:5: warning: no return statement in function returning non-void [-Wreturn-type]\n }\n ^\n\n(thanassis)$ ./a.out \n\n(thanassis)$ gdb ./a.out\nGNU gdb (Ubuntu 10.2-0ubuntu1~18.04~2) 10.2\nCopyright (C) 2021 Free Software Foundation, Inc.\nLicense GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\nThis is free software: you are free …Run Code Online (Sandbox Code Playgroud) 我想知道条件语句(例如if语句)的正确用法,以避免未定义的行为。让我们从一个例子开始:
uint8_t x = 0;
bool y = false;
bool z = false;
if ((x == 135) and !y and !z) {
//do something
}
else if ((x == 135) and y) {
x = 5;
z = true;
}
else if ((x == 5) and z) {
x = 135;
z = false;
}
else {
//do something
}
Run Code Online (Sandbox Code Playgroud)
现在,是否通过不将所有3个变量都包含在每个条件中来获得不确定的行为?是否将所有未说明的条件都放入else语句中?如果是这样,如果我放弃else语句会怎样?我有完全相同的if语句(在更复杂的情况下),而且我似乎每次都不会进入正确的语句。
如果有此规定,请赐教。
c++ conditional if-statement undefined-behavior language-lawyer
c++ ×9
pointers ×4
c ×3
arrays ×1
conditional ×1
constructor ×1
for-loop ×1
gcc ×1
if-statement ×1
nullptr ×1
vector ×1