小编use*_*065的帖子

如何在C++中将输入输入到字符指针中

我正在使用以下代码。

#include <iostream>
using namespace std;

int main() {
  char* str;
  std::cin>>str;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我显然明白 str 只是一个指向任何内容的指针,因此无法接受输入。但是有没有办法将输入输入到字符指针中?

c++ input char

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

如果我们在c ++中重载构造函数,默认构造函数是否仍然存在?

可能重复:
为什么默认的无参数构造函数在创建带参数的构造函数时会消失

我写了以下程序

#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&)

c++ constructor default-constructor

3
推荐指数
2
解决办法
6151
查看次数

奇怪的行为在c

可能重复:
可以在其范围之外访问局部变量的内存吗?

我最近遇到了以下代码:

#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().

我的问题是:

  1. 为什么指向内部声明的数组xyz().

  2. 函数xyz()执行后声明的数组应该超出范围.为什么没有发生?

c arrays pointers

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

c ++中对象属性的内存分配

class A {
  public:
    int i;
};

int main() {
  A *obj = new A();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

在下面的代码中,当创建A的对象时,它是在堆中创建的,但是在obj中创建的i的内存在哪里?它是在堆或堆栈上创建还是有不同的行为?

c++ memory-management

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

在c ++中用空格读取输入

目前我正在读取带有空格的输入.

int main() {
  char str[100];
  string st;
  cin.getline(str,100);
  st=str;
}
Run Code Online (Sandbox Code Playgroud)

我想利用字符串附带的函数,所以我将输入读入字符串.有没有其他方法可以直接将输入读入字符串,也允许空格.

c++ string input

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

这个语法是什么意思

以下代码编译.但是,如果我编写代码来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)

c++ most-vexing-parse

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

无法编辑外部变量

我写了以下代码

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)

c++ extern

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

遇到c ++ stl列表问题

我有以下功能.

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是两个列表.

c++ stl list

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