Solved

#include/#pragma once/#ifndef Question

Posted on 2004-04-26
7
740 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now