7

I have this function declaration and definition..

definition


void loadFromFile(
    string const&   fileName,
    Frames&         frames,
    ostream&        log =std::clog
    )
{
    using std::endl;
    using std::ifstream;

    string const    streamDescription   = "text data file " + fileName;

    log << "Opening " << streamDescription << " for reading..." << endl;

    ifstream    stream( fileName.c_str() );
    (!stream.fail())
        || throwX( S() << "Error opening " << streamDescription << "." );

    loadFrom( stream, frames, streamDescription, log );


}

declaration


void  loadFrom(
  istream& stream,
  Frames& frames,
  string const& streamName = "a text stream",
 // ostream should also have default parameter as streamName
  ostream& log  =std::clog); //std::clog create an object for ostream

void loadFromFile(
  string const& fileName,
  Frames& frames,
  ostream&  log =std::clog);

Main


void cppMain( int argc, char const* const argv[] )
{
    (argc == 1) || throwX( S()
        << "Usage: "
        << argv[0] << " <file1.txt>"
);


    soundData::Frames  testFrames;


    soundData::loadFromFile( argv[0], testFrames );

   // doTimeWarping( templateFrames, testFrames );
    cout << "Done." << endl;
}
int main (int argc, char* argv[])
{
  try
    {
        cppMain( argc, argv );
        return EXIT_SUCCESS;
    }
    catch( exception const& x )
    {
        cerr << "!" << x.what() << endl;
    }
    return EXIT_FAILURE;
}

class defination

namespace soundData{

  //-------------------------- FeatureVector:
int FeatureVector::count()const
{
    return values_.size(); 
}

double FeatureVector::operator[](int i)const
{
    return element(i, values_);
}
 FeatureVector::FeatureVector( int n )
    : values_( n )
{}

 /*==================Frame====================================*/
 Frame::Frame( int nFeatures )
    : features( nFeatures )
{}

 /*===================Frames==========================*/

int Frames::count() const
{
    return frames_.size();
}

int Frames::nFeaturesPerFrame() const
{
    return nFeaturesPerFrame_;
}

Frame const& Frames::operator[]( int i ) const
{
    return element( i, frames_ );
}


Frames::Frames( int n )
    : nFeaturesPerFrame_( n )
{}
/*============loading the frames ===============*/
 void loadFromFile( string const& fileName,                     Frames& frames,                     ostream& log) 
{
    using std::endl;
    using std::ifstream;

    string const    streamDescription   = "text data file " + fileName;

    log << "Opening " << streamDescription << " for reading..." << endl;

    ifstream    stream( fileName.c_str() );
    (!stream.fail())
        || throwX( S() << "Error opening " << streamDescription << "." );

    loadFrom( stream, frames, streamDescription, log );


}

} // namespace sounddata

error


Error   1   error C2572: 'soundData::loadFromFile' : redefinition of default parameter : parameter 3    c:lacture\loading frames\loading frames\sounddata.cpp   111 1   loading frames

Error   2   error LNK2019: unresolved external symbol "void __cdecl soundData::loadFrom(class std::basic_istream<char,struct std::char_traits<char> > &,class soundData::Frames &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_ostream<char,struct std::char_traits<char> > &)" (?loadFrom@soundData@@YAXAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAVFrames@1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@3@AAV?$basic_ostream@DU?$char_traits@D@std@@@3@@Z) referenced in function "void __cdecl soundData::loadFromFile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class soundData::Frames &,class std::basic_ostream<char,struct std::char_traits<char> > &)" (?loadFromFile@soundData@@YAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AAVFrames@1@AAV?$basic_ostream@DU?$char_traits@D@std@@@3@@Z)   C:\loading frames\soundData.obj loading frames

What's wrong with it? I am loading only one file, so argc should be 1. But then why is that resulting in an error?

Please also tell me what I should do to read the parameters (int argc, char* argv[]) in main().

I think i did not understand it.

Ryan Wersal
  • 3,210
  • 1
  • 20
  • 29
RidaSana
  • 553
  • 2
  • 6
  • 15
  • Now tell me the story about `loadFrom`? Is it static free function? or its a member of class or what? – Nawaz May 15 '11 at 10:17
  • 3
    I find it very frustrating that in the edited post, the error is related to `loadFrom` function, but you didn't post its definition. and where its defined. Nothing! – Nawaz May 15 '11 at 10:18
  • @Nawaz: here loadFrom is neither a static function nore a member of class.. its just a non static function,... you are right i did not add laodFrom function... – RidaSana May 15 '11 at 11:05

3 Answers3

30

Mention the default value for the parameter in the declaration ONLY:

//declaration  with default parameter
void loadFromFile( string const& fileName, 
                   Frames& frames, 
                   ostream& log =std::clog);

Don't mention the default value in the definition:

//definition
void loadFromFile( string const& fileName, 
                   Frames& frames, 
                   ostream& log) 
{
     //....
}

Now its perfect. It should compile now!

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • @Miss: This is how the language is designed, perhaps? And if you want to study this, then get the C++ Standard book. – Nawaz May 15 '11 at 09:18
  • @Miss: there is another interesting story about default parameters. See this : [Default argument in the middle of parameter list?](http://stackoverflow.com/questions/5637679/default-argument-in-the-middle-of-parameter-list) – Nawaz May 15 '11 at 09:19
  • @Miss: Maybe, that book doesn't contain this information. Also, I didn't only say `C++ book`. I said `C++ Standard`. – Nawaz May 15 '11 at 09:20
  • @Miss: What error? That error would be somewhere else, and not related to default parameter anymore. – Nawaz May 15 '11 at 09:21
  • 1
    @Miss: Is `loadFromFile` is non-static member function of a class? – Nawaz May 15 '11 at 09:23
  • @Miss: You accepted the answer. Should I assume that your solved this another problem yourself, or should I look into this further? – Nawaz May 15 '11 at 09:25
  • @Miss: that means, you have to create an instance of the class,and call the function using this instance. Something like this : `soundData obj; obj.loadFromFile(/*..params..*/);`. – Nawaz May 15 '11 at 09:27
  • @nawaz: i maked the function as static in class .. but another error strang is that its in standard class--> vector.... – RidaSana May 15 '11 at 09:32
  • @Miss: I didn't understand your last comment. – Nawaz May 15 '11 at 09:34
  • @nawaz: i maked the function as static in class .. but another strange error is in **vector class** , – RidaSana May 15 '11 at 09:38
  • @Miss: How would I know what "another strange error" in the vector class is? You should edit your post and, copy paste the error there in the post. – Nawaz May 15 '11 at 09:41
  • Error 1 error C2129: static function 'void soundData::loadFromFile(const std::string &,soundData::Frames &,std::ostream &)' declared but not defined c:\program files\microsoft visual studio 10.0\vc\include\vector 1269 1 loading frames – RidaSana May 15 '11 at 09:44
  • @Miss: Post the class definition in your post, with the error. Please don't post error and code here. its difficult to read here. – Nawaz May 15 '11 at 09:45
  • @nawaz: actaully i am trying to load an input file... input file have numbers as frames ,, and each frame have features(numbers) . – RidaSana May 15 '11 at 09:52
  • 1
    @Miss: You've everything messed up. `loadFromFile` is NOT a member of any class. remove the `static` keyword its definition, as `static` means the function is visible only within that file. It cannot be called from any other source file. – Nawaz May 15 '11 at 09:55
4

As already mentioned in other answers,

Error Cause:
The cause of the error is that you have mentioned default value arguments in definition of the function. They should be only mentioned in the function declaration and not the definition.

To answer your second question.
please tell me what should i read for parameters (int argc, char argv[]) in main()?*

argc => The number of arguments supplied to the program (the program included)
argv => An array of pointers to the strings which are the arguments supplied to program—its type is ‘array of pointer to char’

These arguments are passed to the program by the host system's command line interpreter or job control language.

You can read this to understand passing arguments to main.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • so i have to load only one input thatsy i think argc should be equal to 1. and argv(1) will get one as array element .. right? – RidaSana May 15 '11 at 09:28
  • @Miss: The first element in argv is always the name of the program itself, so argc is always at least 1, argv[0] is name of your program, and then what follows is other arguments passed to the program. – Alok Save May 15 '11 at 09:34
  • let me study that link first then i will see agian the arg problem of main ..thanks for that link – RidaSana May 15 '11 at 09:37
1

In your loadFromFile function definition you should write

void loadFromFile( string const& fileName, Frames& frames, ostream& log) {/*...*/}

You shouldn't specify default paramater value in function definition.

beduin
  • 7,943
  • 3
  • 27
  • 24