C++ 表达式必须具有类类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6547602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:20:33  来源:igfitidea点击:

Expression must have class type

c++classnew-operator

提问by adrianton3

I have't coded in c++ for some time and I got stuck when I tried to compile this simple snippet:

我已经有一段时间没有用 C++ 编码了,当我试图编译这个简单的代码片段时,我卡住了:

class A
{
  public:
    void f() {}
};

int main()
{
  {
    A a;
    a.f(); // works fine
  }

  {
    A *a = new A();
    a.f(); // this doesn't
  }
}

回答by Kos

It's a pointer, so instead try:

这是一个指针,所以请尝试:

a->f();

Basically the operator .(used to access an object's fields and methods) is used on objects and references, so:

基本上运算符.(用于访问对象的字段和方法)用于对象和引用,因此:

A a;
a.f();
A& ref = a;
ref.f();

If you have a pointer type, you have to dereference it first to obtain a reference:

如果你有一个指针类型,你必须先取消引用它以获得引用:

A* ptr = new A();
(*ptr).f();
ptr->f();

The a->bnotation is usually just a shorthand for (*a).b.

a->b符号通常只是 的简写(*a).b

A note on smart pointers

关于智能指针的说明

The operator->can be overloaded, which is notably used by smart pointers. When you're using smart pointers, then you also use ->to refer to the pointed object:

所述operator->可以被重载,这是值得注意的是使用智能指针。当您使用智能指针时,您还可以使用->来引用指向对象:

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

回答by Sebastian Mach

Allow an analysis.

允许分析。

#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.
}

As a general advice:

作为一般建议:

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.

As a rule of thumb: If you need to manage memory on your own, there is generally a superiour manager or alternative available already, one that follows the RAII principle.

根据经验:如果您需要自己管理内存,通常已经有一个高级管理器或替代方案可用,遵循 RAII 原则。

回答by Ozair Kafray

Summary: Instead of a.f();it should be a->f();

总结:而不是a.f();它应该是a->f();

In main you have defined aas a pointer to object ofA, so you can access functions using the ->operator.

在 main 中,您已将a定义为指向A对象的指针,因此您可以使用运算符访问函数。->

An alternate, but less readable way is (*a).f()

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

a.f()could have been used to access f(), if awas declared as: A a;

a.f()如果a被声明为: A a;

回答by Dark Falcon

ais a pointer. You need to use->, not .

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