• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 652
  • Last Modified:

Coding problem in making a record file C++

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
djchiena
Asked:
djchiena
  • 4
  • 2
  • 2
1 Solution
 
bcladdCommented:
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
 
meow00Commented:
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
 
djchienaAuthor Commented:
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
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.

 
bcladdCommented:
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
 
meow00Commented:
Understood ..... thank you ! .... meow.
0
 
bcladdCommented:
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
 
bcladdCommented:
So, djchiena- Did this solve your problem? Do you have further need for assistance on this?
0
 
djchienaAuthor Commented:
Yeah you solved it...thanks!!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now