What is wrong with the default settings?

I have this declaration and definition of this function.

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 ); } 

announcement


 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); 

the 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 protection

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 p>

Mistake


 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 about him? I upload only one file, so argc should be 1. But then why does this lead to an error?

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

I think I did not understand this.

+7
source share
3 answers

Specify 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); 

Do not mention the default value in the definition:

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

Now he is perfect. Now it should compile!

+30
source

As mentioned in other answers,

Error Cause:
The reason for the error is that you specified the default arguments in the function definition. They should only be mentioned in a function declaration, not in a definition.

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

argc => Number of arguments provided to the program (including the program)
argv => An array of pointers to strings that are arguments passed to the program type is an "array of pointers to char

These arguments are passed to the program by the shell or the job control language.

You can read this to understand how to pass main arguments.

+4
source

In the definition of the loadFromFile function you should write

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

You must not specify a default parameter value in the function definition.

+1
source

All Articles