Multiple definitions of class functions?

Hi, I am trying to link together 5 files for my upcoming project, and I keep getting the same error messages telling me I have multiple definitions of class functions. The five files I am linking are:

Sequence.cpp and Sequence.h: These files describe a class Sequence and are saved elsewhere on the computer. I have to use the direct pathname to the files, but I can't edit them.

vlifunctions.cpp and vli.h: These files describe the class VLI (very long integer) using the Sequence class.

project4B.cpp: My driver file.

Here is my makefile and how I am including the Sequence files:

Makefile:
1
2
3
4
5
6
7
8
9
10
11
12
p4B.exe:        project4B.o  vli.o
        g++ -Wall -o p4B.exe project4B.o vli.o

#   Compile the student's vlifunctions.cpp files, to create vli.o

vli.o:  vlifunctions.cpp vli.h
        g++ -Wall -c vlifunctions.cpp -o vli.o

#   Compile the student's main program to create  project4B.o

project4B.o:    project4B.cpp vli.h
        g++ -Wall -c project4B.cpp


vli.h:
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
#ifndef VLI_
#define VLI_

#include <cstdlib>
#include <new>
#include "/user/cse232/Examples/example32.sequence.h"

class VLI
        {
        public:
                VLI();  //Constructor
                void read(string line); 
                void execute(char Operation);
                string sum();
                string difference();
                string product();
                string quotient();
                string greaterthan();
                string lessthan();
                string equal();
        private:
                Sequence Num1;
                Sequence Num2;
                bool Num1_isneg;
                bool Num2_isneg;
                char Operation;
                string Output;

                };
#endif 


vlifunctions.cpp: (Beginning; functions not included)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using namespace std;
#include <fstream>
#include <cmath>
#include <stdlib.h>
#include <iostream>
#include "/user/cse232/Examples/example32.sequence.h"
//#include "vli.h"

//----------------Constructor-----------------

VLI::VLI()
{
//cout << "Constructor initialized." << endl;
Sequence Num1;
Sequence Num2;
bool Num1_isneg = false;
bool Num2_isneg = false;
char Operation;
string Output;
}


In vlifunctions.cpp, I have #include "vli.h" as a comment because if I include it, I get an error message for each function telling me it has multiple definitions. If I don't have that line, though, it tells me that the class 'VLI' is not defined in this scope. Could anyone tell me what I am doing wrong? Thanks so much!

Hey,

what is inside "/user/cse232/Examples/example32.sequence.h"??

and could you please post the error message from the compiler please.
Last edited on
The error messages I'm getting look like this:

vli.o: In function `VLI::read(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
vlifunctions.cpp:(.text+0x0): multiple definition of `VLI::read(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
project4B.o:project4B.cpp:(.text+0x150): first defined here
vli.o: In function `VLI::execute(char)':
vlifunctions.cpp:(.text+0xc): multiple definition of `VLI::execute(char)'
project4B.o:project4B.cpp:(.text+0x15c): first defined here

But I'm basically getting one for each function. Also, the sequence.h file starts like this:

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

/******************************************************************************
   Example #32 -- Interface file for type "Sequence" (array version)
******************************************************************************/

#ifndef SEQUENCE_
#define SEQUENCE_

typedef int EType;

/*-----------------------------------------------------------------------------
   Type "Sequence" represents an unordered sequence of N items (in positions 0
   to N-1), where each item is of type "EType".  Duplicate items are allowed.

   Type "EType" must support "operator=()" and "operator==()".
-----------------------------------------------------------------------------*/

class Sequence
{
  public:

    // Initialize the sequence
    //
    Sequence();

    // De-initialize the sequence
    //
    ~Sequence();

    // Re-initialize the sequence
    //
    void reset();

    // Initialize the sequence by copying an existing sequence
    //
    Sequence( const Sequence& Source );


And so on, defining the functions that go into the class Sequence that I'm using in my VLI class.
Hmmm... I don't see a mistake so far :-/

Could you show me project4B.cpp

Maybe you set 2 #endif's at the end of sequence.h and then include it in vli.h.

Try a new line at the end of vli.h

Topic archived. No new replies allowed.