Solved

#include/#pragma once/#ifndef Question

Posted on 2004-04-26
7
757 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

Technology Partners: 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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

724 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