Solved

Coding problem in making a record file C++

Posted on 2003-12-09
8
602 Views
Last Modified: 2012-06-27
I made a header file for my program that directs read and write capabilities of records.  when i run my program, this particular header file is giving me two errors in Visual C++ thus it won't run.  Here are the two errors:

\hashing\hashing2\recfile.h(30) : error C2572: 'Read' : redefinition of default parameter : parameter 2
        \hashing\hashing2\recfile.h(14) : see declaration of 'Read'
\hashing\hashing2\recfile.h(39) : error C2572: 'Write' : redefinition of default parameter : parameter 2
        \hashing\hashing2\recfile.h(15) : see declaration of 'Write'

attached is my source code...Can someone pleeeeeeeeeeeeeease help me out! THIS IS URGENT!!!  I am assuming that the same error will happen for the Append() function so please take a look at that as well.

#ifndef RECFILE_H
#define RECFILE_H

#include "buffile.h"
#include "iobuffer.h"
// template class to support direct read and write of records
// The template parameter RecType must support the following
//      int Pack (IOBuffer &); pack record into buffer
//      int Unpack (IOBuffer &); unpack record from buffer

template <class RecType>
class RecordFile: public BufferFile
{public:
      int Read (RecType & record, int recaddr = -1);
      int Write (const RecType & record, int recaddr = -1);
      int Append (const RecType & record);
      RecordFile (IOBuffer & buffer): BufferFile (buffer) {}
};

// template method bodies
template <class RecType>
int RecordFile<RecType>::Read (RecType & record, int recaddr = -1)
{
      int writeAddr, result;
      writeAddr = BufferFile::Read (recaddr);
      if (!writeAddr) return -1;
      result = record . Unpack (Buffer);
      if (!result) return -1;
      return writeAddr;
}  <-------------------------------This is the line where it's producing the error

template <class RecType>
int RecordFile<RecType>::Write (const RecType & record, int recaddr = -1)
{
      int result;
      result = record . Pack (Buffer);
      if (!result) return -1;
      return BufferFile::Write (recaddr);
} <---------------------------This is the line where it's producing the error

template <class RecType>
int RecordFile<RecType>::Append (const RecType & record)
{
      int result;
      result = record . Pack (Buffer);
      if (!result) return -1;
      return BufferFile::Append();
}

#endif
0
Comment
Question by:djchiena
  • 4
  • 2
  • 2
8 Comments
 
LVL 11

Accepted Solution

by:
bcladd earned 500 total points
Comment Utility
Don't include the default values when defining the variables. Your code should read:

#ifndef RECFILE_H
#define RECFILE_H

#include "buffile.h"
#include "iobuffer.h"
// template class to support direct read and write of records
// The template parameter RecType must support the following
//     int Pack (IOBuffer &); pack record into buffer
//     int Unpack (IOBuffer &); unpack record from buffer

template <class RecType>
class RecordFile: public BufferFile
{public:
     int Read (RecType & record, int recaddr = -1);
     int Write (const RecType & record, int recaddr = -1);
     int Append (const RecType & record);
     RecordFile (IOBuffer & buffer): BufferFile (buffer) {}
};

// template method bodies
template <class RecType>
int RecordFile<RecType>::Read (RecType & record, int recaddr) // changed
{
     int writeAddr, result;
     writeAddr = BufferFile::Read (recaddr);
     if (!writeAddr) return -1;
     result = record . Unpack (Buffer);
     if (!result) return -1;
     return writeAddr;
}

template <class RecType>
int RecordFile<RecType>::Write (const RecType & record, int recaddr) // changed
{
     int result;
     result = record . Pack (Buffer);
     if (!result) return -1;
     return BufferFile::Write (recaddr);
}

template <class RecType>
int RecordFile<RecType>::Append (const RecType & record)
{
     int result;
     result = record . Pack (Buffer);
     if (!result) return -1;
     return BufferFile::Append();
}

#endif


Both changed lines are marked.
0
 
LVL 1

Expert Comment

by:meow00
Comment Utility
Hi Experts,
    Just have a related question about the above code. In general, how do we test or debug a header file (*.h) without a main code (like above) ? Is that the same for visual C++ and C++ ? thanks a lot !

meow....
0
 

Author Comment

by:djchiena
Comment Utility
the only way to debug a header file is by compiling the main code.  When I tried to compile my code from the main file, it pointed me to errors on the header file.
0
 
LVL 11

Expert Comment

by:bcladd
Comment Utility
meow00-
I just dumped the posted code into a .cpp file with an empty main() and instantiated a RecordFile<int>. Had to comment out some of the dependencies (buffile.h and iobuffer.h and the classes they contained).

I had figured out what the problem was by inspection but figured a quick compile cycle to show that I could fix it would be a good thing before posting a solution.

-bcl
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Expert Comment

by:meow00
Comment Utility
Understood ..... thank you ! .... meow.
0
 
LVL 11

Expert Comment

by:bcladd
Comment Utility
Just reread my original post. Both of the fixed lines are correct but I misstated the problem:

When you separate the declaration and definition of a FUNCTION (I said variable) with default values for the parameters the default values go on the delcaration ONLY. Thus the bug was the repeat of the "int recaddr = -1" or, more specifically, the " = -1" when defining Read and Write. Append doesn't have the problem because it has no default parameters.

Hope what I meant is clearer.
-bcl
0
 
LVL 11

Expert Comment

by:bcladd
Comment Utility
So, djchiena- Did this solve your problem? Do you have further need for assistance on this?
0
 

Author Comment

by:djchiena
Comment Utility
Yeah you solved it...thanks!!!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

  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 …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

728 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

10 Experts available now in Live!

Get 1:1 Help Now