Solved

#include/#pragma once/#ifndef Question

Posted on 2004-04-26
7
751 Views
Last Modified: 2012-05-04
Hi,

I have a program which only calls a class TransRecords.h once, but I'm getting the following error:

Bank.obj : error LNK2005: "public: void __thiscall TransRecords::addRecord(double,int,double)" (?addRecord@TransRecords@@QAEXNHN@Z) already defined in Account.obj

AND

TransRecords.obj : error LNK2005: "public: void __thiscall TransRecords::addRecord(double,int,double)" (?addRecord@TransRecords@@QAEXNHN@Z) already defined in Account.obj

Here is my TransRecords.h code:

#pragma once

class TransRecords {

public:
      TransRecords();
      ~TransRecords();
      void addRecord(double transAmt, int transType, double endingBalance);

private:
      double transAmt;
      int transType;
      double endingBalance;

};

TransRecords::TransRecords() { //Constructor

}

TransRecords::~TransRecords() { //Destructor

}

void TransRecords::addRecord(double newTransAmt, int newTransType, double newEndingBalance) {

      transAmt = newTransAmt;
      transType = newTransType;
      endingBalance = newEndingBalance;

}


Any ideas on what I'm doing wrong?  Please let me know if you need to see more code.  I have many files.

Ryan
0
Comment
Question by:RySk8er30
7 Comments
 
LVL 6

Expert Comment

by:Mafalda
ID: 10919721
Did you try ?

#ifndef _TRANSRECORDS_H
#define _TRANSRECORDS_H

class TransRecords {

public:
     TransRecords();
     ~TransRecords();
     void addRecord(double transAmt, int transType, double endingBalance);

private:
     double transAmt;
     int transType;
     double endingBalance;

};

TransRecords::TransRecords() { //Constructor

}

TransRecords::~TransRecords() { //Destructor

}

void TransRecords::addRecord(double newTransAmt, int newTransType, double newEndingBalance) {

     transAmt = newTransAmt;
     transType = newTransType;
     endingBalance = newEndingBalance;

}

#endif // _TRANSRECORDS_H
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10919738
Your compiler may not support #pragma once (which compiler are you using?), so try the "normal" include guard with #ifndef/#define/#endif
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10919756
Don't put the implementations of your methods into the header file. Create a separate .cpp file that only contains the actual implementation of your methods.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 50 total points
ID: 10919782
A header file should _NEVER_ contain anything executable (with the exception of functions that are declared inline), or variable definitions. These things should all go into a file that gets compiled (e.g. .cpp).
0
 
LVL 5

Expert Comment

by:allmer
ID: 10919866
Are you sure you don't have the same function in your Account class? Maybe it's only a name conflict.

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10919869
The quickest solution is inlining the construct, destruct and add function

inline
TransRecords::TransRecords() { //Constructor

}

inline
TransRecords::~TransRecords() { //Destructor

}

inline
void TransRecords::addRecord(double newTransAmt, int newTransType, double newEndingBalance) {

     transAmt = newTransAmt;
     transType = newTransType;
     endingBalance = newEndingBalance;

}

Regards, Alex
0
 

Author Comment

by:RySk8er30
ID: 10919899
Hi,

Man, I must have been tired when writing this program.  I didn't mean to copy the implementation into the header file.  Sorry for the hassle.

Ryan
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question