Trouble with Class Files

Currently I am creating a program that uses my own defined class to compute a polynomial function based on user input in the application file (which is yet to be complete. Upon compiling without the linker, there are no problems. Compiling the term.cpp file with the linker however results in:

"Undefined first referenced
symbol in file
main /opt/csw/gcc3/lib/gcc/sparc-sun-solaris2.8/3.4.5/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status"

and compiling the termApp.cpp file results in:

"Undefined first referenced
symbol in file
Term::Term(int, int) /var/tmp//ccR6FCVq.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status"

I looked all over the files for possibilities that could cause these errors, but I'm stumped. Thanks in advance!!
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
//File:           term.h
//Author:         Eric Howard
//Course:         CSC136 030
//Date:           15 February 2009
//Assignment:     Program #2
//File Purpose:     Contains the declaration of the Term class.
//Program Purpose:  This program's purpose is to implement a created class
//                  to store and manipulate variables of a polynomial function.

#include <iostream>
using namespace std;

#ifndef TERM_H
#define TERM_H

class Term
{
 public:
  Term();                             //assigns 0 to coefficent and exponent
  Term(int c, int e); //passes initial values to coefficent/exponent

  int getCoefficient() const;    //returns value for coefficient
  int getExponent() const;       //returns value for exponent

  void setCoefficient(int aC);   //sets value for coefficent
  void setExponent(int aE);      //sets value for exponent

  int evaluate(int xVal);        //passed xVal value and returns term value


  bool operator<(const Term &right); //compares: one term is less than another if exponent is smaller
  bool operator>(const Term &right); //compares: one term is greater than another if exponent is larger
  bool operator==(const Term &right); //compares: two terms are equal if their exponents are equal
  bool operator!=(const Term &right); //compares: two terms are not equal if their exponents are not equal

  Term operator+(const Term &right); //adds two terms to produce a new term

 private:
    int coefficient, exponent, term;

    };

    ostream &operator<<(ostream &output, const Term &term);  //writes term to output stream
    istream &operator>>(istream &input, Term &term);         //reads term from input stream

#endif 


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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//File:           term.cpp
//Author:         Eric Howard
//Course:         CSC136 030
//Date:           15 February 2009
//Assignment:     Program #2
//File Purpose:     Contains the definition of the Term class.
//Program Purpose:  This program's purpose is to implement a created class
//                  to store and manipulate variables of a polynomial function.

#include "term.h"
#include <iostream>
using namespace std;

  //Constructors
  Term::Term()
  {
    coefficient = 0;
    exponent = 0;
  }

  Term::Term(int c, int e)
  {
    coefficient = c;
    exponent = e;
  }

  //Gets and Sets
  int Term::getCoefficient() const {return coefficient;}

  int Term::getExponent() const {return exponent;}

  void Term::setCoefficient(int aC) {coefficient = aC;}

  void Term::setExponent(int aE) {exponent = aE;}

  //evaluate
  //Purpose: passed a value for xVal and returns the value of the term
  //Parameters: xVal - value for variable x
  //Return: term
  int Term::evaluate(int xVal)
  {
    term = coefficient + xVal ^ exponent;
    return term;
  }
 //<: one term is less than another if its exponent is smaller
  bool Term::operator<(const Term &right)
  {
    if(exponent < right.exponent)
      return true;

    if(right.exponent < exponent)
      return false;
  }

  //>: one term is greater than another it its exponent is larger
  bool Term::operator>(const Term &right)
  {
    if(exponent > right.exponent)
      return true;

    if(right.exponent > exponent)
      return false;
  }

  //==: two terms are equal to each other if their exponents are equal
  bool Term::operator==(const Term &right)
  {
    if(exponent == right.exponent)
      return true;
    else
      return false;
  }

  //!=: two terms are not equal if their exponents are not equal
  bool Term::operator!=(const Term &right)
  {
    if(exponent != right.exponent)
      return true;
    else
      return false;
  }


  //Arithemetic
  //+: adds two terms to produce another term
  Term Term::operator+(const Term &right)
  {
    Term tempTerm;

    tempTerm.coefficient = coefficient + right.coefficient;
    tempTerm.exponent = right.exponent;
    return tempTerm;
  }

    //Stream Operators
    //<<: operator for writing output to a stream
    ostream &operator<<(ostream &output, const Term &term)
    {
      output << term.getCoefficient() << "x" << term.getExponent();
      return output;
    }

    //>>; operator for reading output from a stream
    istream &operator>>(istream &input, Term &term)
    {
      int coefficient, exponent;

      //Prompt user for Coefficient
      cout << "Coefficient: ";
      input >> coefficient;

      //Prompt user for Exponent
      cout << "Exponent: ";
      input >> exponent;

      return input;
    }

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
//File:           termApp.cpp
//Author:         Eric Howard
//Course:         CSC136 030
//Date:           15 February 2009
//Assignment:     Program #2
//File Purpose:     Contains the Application for the Term class.
//Program Purpose:  This program's purpose is to implement a created class
//                  to store and manipulate variables of a polynomial function.

#include <iostream>
#include "term.h"
using namespace std;

int main()
  {
    int aC, aE;

    aC = 5;
    aE = 3;

    Term newterm(aC, aE);


  return (0);

  }

One cannot compile code with a linker.

In your case, when you compile termApp.cpp you need to tell the compiler not to try to link it (create an executable). Likewise with term.cpp.

With gcc,

g++ -C term.cpp
g++ -C termApp.cpp
g++ -o termApp term.o termApp.o

-C means compile but no link.
Topic archived. No new replies allowed.