我正在使用以下代码。
#include <iostream>
using namespace std;
int main() {
char* str;
std::cin>>str;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我显然明白 str 只是一个指向任何内容的指针,因此无法接受输入。但是有没有办法将输入输入到字符指针中?
我写了以下程序
#include <iostream>
class A {
public:
A(int i) {std::cout<<"Overloaded constructor"<<std::endl;}
}
int main() {
A obj;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译程序时,我收到以下错误:
没有匹配函数来调用A :: A()候选者:A :: A(int)A :: A(const A&)
可能重复:
可以在其范围之外访问局部变量的内存吗?
我最近遇到了以下代码:
#include <stdio.h>
int* abc () {
int a[3] = {1,10,100};
return a;
}
int* xyz () {
int b[1] = {222};
return b;
}
int main() {
int *a, *b;
a = abc();
b = xyz();
printf("%d\n", *a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是222.'a'指向在里面声明的数组xyz().
我的问题是:
为什么指向内部声明的数组xyz().
函数xyz()执行后声明的数组应该超出范围.为什么没有发生?
class A {
public:
int i;
};
int main() {
A *obj = new A();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在下面的代码中,当创建A的对象时,它是在堆中创建的,但是在obj中创建的i的内存在哪里?它是在堆或堆栈上创建还是有不同的行为?
目前我正在读取带有空格的输入.
int main() {
char str[100];
string st;
cin.getline(str,100);
st=str;
}
Run Code Online (Sandbox Code Playgroud)
我想利用字符串附带的函数,所以我将输入读入字符串.有没有其他方法可以直接将输入读入字符串,也允许空格.
以下代码编译.但是,如果我编写代码来test使用jar它调用方法,则会给我一个编译错误.这里真的发生了什么.
#include <iostream>
using namespace std;
class A {
public:
void test() {
cout << "working" << endl;
}
};
int main() {
A foo;
A jar();
}
Run Code Online (Sandbox Code Playgroud) 我写了以下代码
head.h
int i = 0;
Run Code Online (Sandbox Code Playgroud)
将sample.cpp
#include <stdio.h>
#include "head.h"
extern int i;
i = 20;
int main() {
printf("%d \n",i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译sample.cpp时,编译器抛出以下错误:
sample.c:5:1: warning: data definition has no type or storage class [enabled by default]
sample.c:5:1: error: redefinition of ‘i’
head.h:1:5: note: previous definition of ‘i’ was here
Run Code Online (Sandbox Code Playgroud) 我有以下功能.
void BulletFactory::update(Uint32 ticks) {
std::list<Sprite*>::iterator it = activeBullets.begin();
while (it != activeBullets.end()) {
(*it)->update(ticks);
if(!((*it)->inView())) {
activeBullets.remove(*it);
Sprite* temp = *it;
it++;
inactiveBullets.push_back(temp);
} else {
it++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
当条件!((*it)->inView())正在true有段故障.我无法看到问题.
编辑:忘了提到activeBullets和inactiveBullets是两个列表.