Solved

#include/#pragma once/#ifndef Question

Posted on 2004-04-26
7
742 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

895 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

15 Experts available now in Live!

Get 1:1 Help Now