Error after compiling my lab

I can't figure out why my code is not compiling. I will include the cpp file I am having trouble with. There is one rational7.cpp file, one main.cpp file, and a rational7.h file. For my project these seem to be fine. Except, for my rational7.cpp file I came across these issues when compiling. Any answers that could help me to compile it without any errors is appreciated. Thanks

Here is the error after compiling. (I am using mimir by the way):


rational7.cpp:31:5: error: expected initializer before ‘void’
void input(istream& ins)
^~~~
rational7.cpp:47:2: error: ‘friend’ used outside of class
friend rational product(const rational& op1, const rational& op2);
^~~~~~
rational7.cpp:49:5: error: ‘friend’ used outside of class
friend bool isEqual(const rational& op1, const rational& op2);
^~~~~~

Thank you! and if you guys perhaps see a logic error in my code please point them out to me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  /*  class Rational to practice basics of defining and using classes.
 */
#include <iostream>
#include <string>

using namespace std; 

// ToDo: copy the implementation of rational from Lab6rational.cpp
// and modify according to rational7.h (Done.)

/*
 *    class rational
 *    represents a rational number. Remember rational means ratio-nal
 *    which means there is a numerator and denominator implemented using
 *    integer values. Using good ADT techniques, we have made member
 *    variable private (also known as instance variables) and made member
 *    functions public.
 */
   //rational();
   
   //To do: 
   //Put curly braces
   //Put the rational:: in front of member functions 
   //Get the friend key word but do this first ^
    class rational

    rational(int p, int q)
    
    //constructor that takes two ints as parameter. Verify q is not zero
    
    void input(istream& ins)
    // ToDo: read value for the object from standard input 

    void output(ostream& out)
    // ToDo: display the value of the object to standard output 

    //Todo: declare an accessor (getter function) that returns the numerator
    int getNumerator()const
    
    //Todo: declare an accessor (getter function) that returns the denominator
    int getDenominator()const
	//ToDo: delcare a mutator called Set that sets the current object's value to the n/d.
	void Set(int numer, int denom)
    //ToDo: declare a friend function that calculates the sum of op1 and op2 
    friend rational sum(const rational& op1, const rational& op2);
	//ToDo: declare a friend function that calculates the product of op1 and op2
	friend rational product(const rational& op1, const rational& op2);
	//ToDo: declare a friend function that returns true if op1 == op2.
    friend bool isEqual(const rational& op1, const rational& op2);
    
	//ToDo: declare an object counter for the class.
	 int totalCount();
	 int test();
	 int gcd(int a, int b);
Last edited on
//To do:
//Put curly braces
//Put the rational:: in front of member functions
//Get the friend key word but do this first ^
You are supposed to this. The code right now intentionally won't compile, because it isn't syntactically correct.

Normally, if you're separating things by my_class.h and my_class.cpp, the definition and class goes in the header, and the actual method (member function) implementations go in the .cpp file.

I would read this article: http://www.cplusplus.com/forum/articles/10627/
Look at the example in the above link that shows a class being properly defined in the .h (header) file, and implemented in the .cpp (implementation) file.

1
2
3
4
5
6
7
8
// in myclass.h

class MyClass
{
public:
  void foo();
  int bar;
};

1
2
3
4
5
6
// in myclass.cpp
#include "myclass.h"

void MyClass::foo()
{
}


1
2
3
4
5
6
7
8
//in main.cpp
#include "myclass.h"  // defines MyClass

int main()
{
  MyClass a; // no longer produces an error, because MyClass is defined
  return 0;
}
Last edited on
Maybe read and do the things it tells you to.
1
2
   //To do: 
   //Put curly braces 


Topic archived. No new replies allowed.