Solved

Coding problem in making a record file C++

Posted on 2003-12-09
8
622 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
[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
  • 4
  • 2
  • 2
8 Comments
 
LVL 11

Accepted Solution

by:
bcladd earned 500 total points
ID: 9908483
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
ID: 9908529
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
ID: 9908660
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
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!

 
LVL 11

Expert Comment

by:bcladd
ID: 9908670
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
 
LVL 1

Expert Comment

by:meow00
ID: 9908696
Understood ..... thank you ! .... meow.
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9908729
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
ID: 9909255
So, djchiena- Did this solve your problem? Do you have further need for assistance on this?
0
 

Author Comment

by:djchiena
ID: 9909843
Yeah you solved it...thanks!!!
0

Featured Post

Independent Software Vendors: 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

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. …
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 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.

717 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