Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
表达式必须具有类类型 我在C++中没有编码过一段时间,当我试图编译这个简单代码时,我被卡住了: class A { public: void f() {} }; int main() { { A a; a.f(); // works fine } { A *a = new A(); a.f(); // this doesn't } }_C++_Class_New Operator - Fatal编程技术网

表达式必须具有类类型 我在C++中没有编码过一段时间,当我试图编译这个简单代码时,我被卡住了: class A { public: void f() {} }; int main() { { A a; a.f(); // works fine } { A *a = new A(); a.f(); // this doesn't } }

表达式必须具有类类型 我在C++中没有编码过一段时间,当我试图编译这个简单代码时,我被卡住了: class A { public: void f() {} }; int main() { { A a; a.f(); // works fine } { A *a = new A(); a.f(); // this doesn't } },c++,class,new-operator,C++,Class,New Operator,这是一个指针,因此请尝试: a->f(); 基本上,操作符(用于访问对象的字段和方法)用于对象和引用,因此: A a; a.f(); A& ref = a; ref.f(); 如果您有指针类型,则必须首先取消引用以获得引用: A* ptr = new A(); (*ptr).f(); ptr->f(); a->b符号通常只是(*a).b的简写 关于智能指针的一个注记 操作符->可以重载,这是智能指针最常用的方法。当使用时,还可以使用->引用指向的对象: auto ptr

这是一个指针,因此请尝试:

a->f();
基本上,操作符
(用于访问对象的字段和方法)用于对象和引用,因此:

A a;
a.f();
A& ref = a;
ref.f();
如果您有指针类型,则必须首先取消引用以获得引用:

A* ptr = new A();
(*ptr).f();
ptr->f();
a->b
符号通常只是
(*a).b
的简写

关于智能指针的一个注记
操作符->
可以重载,这是智能指针最常用的方法。当使用时,还可以使用
->
引用指向的对象:

auto ptr = make_unique<A>();
ptr->f();
auto ptr=make_unique();
ptr->f();

摘要:代替
a.f()应该是
a->f()

大体上,您已经将a定义为指向a对象的指针,因此您可以使用
->
操作符访问函数

另一种可读性较差的方法是
(*a).f()

如果
a声明为:

A

a
是指针。您需要使用
->
,而不是

允许分析

#include <iostream>   // not #include "iostream"
using namespace std;  // in this case okay, but never do that in header files

class A
{
 public:
  void f() { cout<<"f()\n"; }
};

int main()
{
 /*
 // A a; //this works
 A *a = new A(); //this doesn't
 a.f(); // "f has not been declared"
 */ // below


 // system("pause");  <-- Don't do this. It is non-portable code. I guess your 
 //                       teacher told you this?
 //                       Better: In your IDE there is prolly an option somewhere
 //                               to not close the terminal/console-window.
 //                       If you compile on a CLI, it is not needed at all.
}
#包括//不包括#包括“iostream”
使用命名空间std;//在这种情况下可以,但永远不要在头文件中这样做
甲级
{
公众:

void f(){cout表示“这不是”的行实际上,OK,让你的问题看起来很混乱。只要开始C++,就必须自动地确定是否使用指针或引用。在我的特定情况下,我需要的只是引用,但是由于某种原因,我通过了一个指针。无论如何,谢谢你的清晰解释。
0) Prefer automatic variables
  int a;
  MyClass myInstance;
  std::vector<int> myIntVector;

1) If you need data sharing on big objects down 
   the call hierarchy, prefer references:

  void foo (std::vector<int> const &input) {...}
  void bar () { 
       std::vector<int> something;
       ...
       foo (something);
  }


2) If you need data sharing up the call hierarchy, prefer smart-pointers
   that automatically manage deletion and reference counting.

3) If you need an array, use std::vector<> instead in most cases.
   std::vector<> is ought to be the one default container.

4) I've yet to find a good reason for blank pointers.

   -> Hard to get right exception safe

       class Foo {
           Foo () : a(new int[512]), b(new int[512]) {}
           ~Foo() {
               delete [] b;
               delete [] a;
           }
       };

       -> if the second new[] fails, Foo leaks memory, because the
          destructor is never called. Avoid this easily by using 
          one of the standard containers, like std::vector, or
          smart-pointers.