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
Solved

Coding problem in making a record file C++

Posted on 2003-12-09
8
616 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
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

861 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