[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Coding problem in making a record file C++

Posted on 2003-12-09
8
Medium Priority
?
644 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 2000 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
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!

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

650 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