之前的预期主要表达.在调用类函数时

Vic*_*vin 3 c++ class

对于课堂,我们正在制作一个分析和经验计算T(n)的程序.我们的函数应该在一个单独的类f中,我们应该使用一个函数来读取文件中的输入以用作"n"并调用函数来打印值.当我尝试将分析函数作为我的print函数的参数调用时,我收到此错误:

p03.cpp:61:23: error: expected primary-expression before â.â token
p03.cpp:61:34: error: expected primary-expression before â.â token
Run Code Online (Sandbox Code Playgroud)

我确信这是一个愚蠢的错字,但我找不到它.是的,我在p03.cpp和F03.cpp中包含了F03.h.以下是导致错误的代码:

void analysis(istream& i) {

//Code Fragment 0 analysis
PrintHead(cout, "Code Fragment 0");
for(;;) {
    int n;
    i >> n;
    if (i.eof()) break;
            //Next line is line 61
    PrintLine(cout, n, f.af00(n), f.ef00(n));
}
}
Run Code Online (Sandbox Code Playgroud)

以下是p03.cpp中的打印功能:

    void PrintHead(ostream& o, string title) {
        o << title << endl;
        o << setw(20) << "n" << setw(20) << "Analytical" << setw(20) << "Empirical";
        o << endl;
    }

    void PrintLine(ostream& o, int n, int a, int e) {
        o << setw(20) << n << setw(20) <<a << setw(20) << e << endl;
    }
Run Code Online (Sandbox Code Playgroud)

这是F03.h中f的类声明:

#ifndef F03_h
#define F03_h 1

#include <cstdlib> 
#include <cstring> 
#include <iostream> 
#include <fstream> 
#include <string> 

class f {

    public:
int ef00(int n);

int af00(int n);

};

#endif
Run Code Online (Sandbox Code Playgroud)

以下是实施:

                            #include <cstdlib> 
            #include <cstring> 
            #include <iostream> 
            #include <fstream> 
            #include <string> 

            #include "F03.h"

            int f::ef00(int n) 
                { int t=0; 
                int sum=0; t++; 
                 int i=0; t++; 
                 while (i<n) { t++; 
                 sum++; t++; 
                 i++; t++; 
                 } t++; 
                 return t; 
                } 

            int f::af00(int n) 
                { return 3*n+3; 
                } 
Run Code Online (Sandbox Code Playgroud)

非常感谢任何见解!

jua*_*nza 5

f::af00并且f::ef00是类的非静态成员f,因此您需要在实例上调用它们.例如

f myf;
PrintLine(cout, n, myf.af00(n), myf.ef00(n));
Run Code Online (Sandbox Code Playgroud)

或者,将方法设为静态,并将其称为f::af00(n)等.

class f 
{
 public:
  static int ef00(int n);
  static int af00(int n);
};
Run Code Online (Sandbox Code Playgroud)

然后

PrintLine(cout, n, f::af00(n), f::ef00(n));
Run Code Online (Sandbox Code Playgroud)