3

For class we are making a program that analytically and empirically calculates T(n). Our functions are supposed to be in a separate class f, and we are supposed to use a function to read input from a file to use as "n" and call the functions to print the values. I am getting this error when I try to call the analysis functions as parameters for my print function:

p03.cpp:61:23: error: expected primary-expression before â.â token
p03.cpp:61:34: error: expected primary-expression before â.â token

I am sure this is a silly typo somewhere but I can't find it. And yes I have included F03.h in p03.cpp and in F03.cpp. Here is the code causing the error:

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));
}
}

Here are the print functions also in 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;
    }

Here is class declaration for f in F03.h:

#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

Here are the implementations:

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

Any insight is greatly appreciated!

Victoria Potvin
  • 127
  • 1
  • 11
  • what is in line 61 of p03.cpp? That's where the compiler is complaining but it's hard to tell where that is. – amdn Mar 22 '14 at 17:24
  • I have edited to make it more obvious. it is the line where I call the PrintLine function inside the loop. – Victoria Potvin Mar 22 '14 at 17:27

2 Answers2

5

f::af00 and f::ef00 are non-static members of class f, so you need to call them on an instance. For example

f myf;
PrintLine(cout, n, myf.af00(n), myf.ef00(n));

Alternatively, make the methods static, and call them as f::af00(n) etc.

class f 
{
 public:
  static int ef00(int n);
  static int af00(int n);
};

and then

PrintLine(cout, n, f::af00(n), f::ef00(n));
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

It looks like you are trying to call a non-static function as a static function, when you invoke PrintLine. Specifically f.af00(n); f is the name of a class, but you're using it like the name of a variable.

Probably you intended to declare the functions in class f to be static, since f does not have any data members. If you did that, you would then invoke the function as f::af00(n).

See this question for more info on the concept of static functions: What is a static function?

Community
  • 1
  • 1
Charlie
  • 44,214
  • 4
  • 43
  • 69