C++ 请求“...”中的成员“...”,它是非类类型“...”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4846959/
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 16:44:54  来源:igfitidea点击:

request for member "..." in "..." which is of non-class type "..."

c++compiler-errors

提问by user596228

I've been hunting around for hours trying to figure out what I'm doing wrong, and I've gotten rid of most of my problems but when I try to compile my code in main()it comes up with this same error message:

我已经搜索了几个小时试图找出我做错了什么,我已经解决了我的大部分问题,但是当我尝试编译我的代码时,main()它出现了同样的错误消息:

request for member "..." in "..." which is of non-class type "..."

and it repeats itself for all of the functions I try to call in main(). What is the problem? I can't figure out where my error is.

对于我尝试调用的所有函数,它都会重复main()。问题是什么?我不知道我的错误在哪里。

I'm using Terminal on a macbook to compile the code.

我在 macbook 上使用终端来编译代码。

Here is my main function:

这是我的主要功能:

//Program1.cpp
//Program1Math test function
#include "Program1Math.h"

int main()
{
  //Create a Program1Math object
  Program1Math myProgram1Math();

  myProgram1Math.setNumber1();
  myProgram1Math.setNumber2();

  myProgram1Math.displayMultiple();
  myProgram1Math.displaySine1();
  myProgram1Math.displayTangent1();
  myProgram1Math.displaySine2();
  myProgram1Math.displayTangent2();
}

Here are the member-function definitions for the class:

以下是该类的成员函数定义:

//Program1Math.cpp
//Program1Math member-function definitions.
#include <iostream>
#include <cmath>
#include "Program1Math.h"
using namespace std;

//constructor makes a Program1Math, adds an blank line
Program1Math::Program1Math()
{
  cout << "/n";
}

//function to assign the first integer to its appropriate location
void Program1Math::setNumber1()
{
  cout << "Please enter the first integer number /n";
  int numberSpot;
  cin >>numberSpot;
  static_cast<double>(numberSpot);
  number1 = numberSpot;
}

//function to assign the second integer to its appropriate location
void Program1Math::setNumber2()
{
  cout << "Please enter the second integer number /n";
  int numberSpot;
  cin >>numberSpot;
  static_cast<double>(numberSpot);
  number2 = numberSpot;
}

//function to find the sine value for a specified number
void Program1Math::calculateSine( double inputNumber )
{
  sineValue = sin( inputNumber ); 
}

//function to find the tangent value for a specified number
void Program1Math::calculateTangent( double inputNumber )
{
  tangentValue = tan( inputNumber );
}

//function to determine if the user-inputted numbers are multiples of each other
void Program1Math::calculateModulus()
{
  int number1Int = static_cast<int>(number1);
  int number2Int = static_cast<int>(number2);
  int modulusValue = number1Int % number2Int;
  if ( modulusValue == 0 )
    multiple = true;
  else
    multiple = false;
}

//function to display the whether the numbers are multiples or not
void Program1Math::displayMultiple()
{
  if( multiple == true )
    cout<< number1 << " is a multiple of " << number2 << "!/n";
  else
    cout<< number1 << "is not a multiple of " << number2 << "./n";
}

//function to display the sine value of the first number
void Program1Math::displaySine1()
{
  calculateSine( number1 );
  cout << "Sine(" << number1 << ") = " << sineValue << "/n";
}

//function to display the sine value of the second number
void Program1Math::displaySine2()
{
  calculateSine( number2 );
  cout << "Sine(" << number2 << ") = " << sineValue << "/n";
}

//function to display the tangent value of the first number
void Program1Math::displayTangent1()
{
  calculateTangent( number1 );
  cout << "Tan(" << number1 << ") = " << tangentValue << "/n";
}

//function to display the tangent value of the second number
void Program1Math::displayTangent2()
{
  calculateTangent( number2 );
  cout << "Tan(" << number2 << ") = " << tangentValue << "/n";
}

here is the header file:

这是头文件:

#include <cmath>
using namespace std;

class Program1Math
{
 public:
  Program1Math();
  void setNumber1();
  void setNumber2();
  void calculateSine( double );
  void calculateTangent( double );
  void calculateModulus();
  void displayMultiple();
  void displaySine1();
  void displaySine2();
  void displayTangent1();
  void displayTangent2();
 private:
  double number1;
  double number2;
  double sineValue;
  double tangentValue;
  bool multiple;
};

回答by engf-010

the line Program1Math myProgram1Math();is interpreted as a function declaration myProgram1Math()returning Program1Math;

该行Program1Math myProgram1Math();被解释为myProgram1Math()返回的函数声明Program1Math

Just use

只需使用

 Program1Math myProgram1Math;

Only use () when a constructor accepts an parameter (without a default argument).

仅当构造函数接受参数(没有默认参数)时才使用 ()。

EDIT : you must compile all the source (.cpp) files that comprise your program. That will produce object files of the same name with a different extension (on windows this is .obj ,for you it's .o).

编辑:您必须编译构成程序的所有源 (.cpp) 文件。这将产生具有不同扩展名的同名目标文件(在 Windows 上这是 .obj ,对你来说是 .o)。

After that all those .o-files must be linked togetcher with some compiler-provided libraries to make your executable program.

之后,.o必须将所有这些-files 与一些编译器提供的库链接到 getcher 以生成可执行程序。

回答by makes

You should create the Program1Mathinstance like this:

您应该Program1Math像这样创建实例:

Program1Math myProgram1Math;

or to allocate the object on the heap, use the newkeyword:

或者在堆上分配对象,使用new关键字:

Program1Math *myProgram1Math = new Program1Math();