Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

#include/#pragma once/#ifndef Question

Posted on 2004-04-26
7
Medium Priority
?
767 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 200 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

604 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