implementing a class error - simple!

Hello All,

I have a very simple class but I cannot create a object from it.

This is the error I get when compiling:

>gmake
g++ -ggdb -c Event.cpp
g++ -ggdb -c main.C
Linking run ...
g++ Event.o main.o -o run
main.o(.text+0x157): In function `main':
/home/test/main.C:7: undefined reference to `Event::Event()'
collect2: ld returned 1 exit status
gmake: *** [run] Error 1


Why am I getting this error?


The header Event.hpp
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
#ifndef Event_h
#define Event_h

#include <iostream>
#include <vector>

class Event
{
   public:

      float npvs;

   protected:

      int pro;

   public:

      Event();
      ~Event();
      void Clear();

};


#endif 


cpp file Event.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "Event.hpp"


// Clean-up whole event
void Event::Clear()
{
   // Rest all variables to default values
   npvs = -1.;
   pro = -1;
}


Makefile
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
######################

CXX           = g++
LD            = g++

#CXXFLAGS      = -fPIC 
CXXFLAGS      = -ggdb
#LDFLAGS       = -m32

LIBS          = 

HDRS          = Event.hpp
SRCS          = main.C Event.cpp
OBJS          = Event.o main.o

PROGRAM       = run

$(PROGRAM):     $(OBJS)
	@echo "Linking $(PROGRAM) ..."
	@echo $(LD) $(LDFLAGS) $(OBJS) $(LIBS) -o $(PROGRAM)
	@$(LD) $(LDFLAGS) $(OBJS) $(LIBS) -o $(PROGRAM)
	@echo "done"


##########################################################

Event.o: Event.hpp

%.o : %.C
	$(CXX) $(CXXFLAGS) -c $<

%.o : %.cpp
	$(CXX) $(CXXFLAGS) -c $<

%.o : %.cxx
	$(CXX) $(CXXFLAGS) -c $<

clean:
	rm *.o *.so

# DO NOT DELETE 

You declared a constructor and destructor but you didn't provide an implementation for either.
Can you provide and example of one? Would I also need a copy constructor if I plan to make a vector of this object?
Given the data in the class above, you don't need a destructor; the default one provided by the compiler will suffice.
Perhaps your default constructor should default initialize the data members to zero. You do not need a copy constructor
since the default one provided by the compiler will suffice.
Thanks!
Topic archived. No new replies allowed.