什么是默认参数错误?

时间:2011-05-15 09:05:17

标签: c++

我有这个函数声明和定义..

定义


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


}

声明


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

主要


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

课程定义

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


}

} //命名空间sounddata

错误


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

它出了什么问题?我只加载一个文件,因此argc应为1.但是为什么会导致错误?

请告诉我应该怎么做才能阅读参数(int argc, char* argv[])  在main()

我想我不明白。

3 个答案:

答案 0 :(得分:30)

仅在声明中提及参数的默认值:

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

请勿在定义中提及默认值:

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

现在它完美无缺。它现在应该编译!

答案 1 :(得分:4)

正如其他答案中已经提到的那样,

错误原因:
错误的原因是您在函数的定义中提到了默认值参数。它们应仅在函数声明中提及,而不是定义。

回答你的第二个问题 请告诉我应该在main()中读取参数(int argc,char argv [])?*

argc =&gt; 提供给程序的参数数量(包括程序)
argv =&gt; 指向字符串的指针数组,这些字符串是提供给program-its类型的参数,是“指向char的指针数组”

这些参数由主机系统的命令行解释器或作业控制语言传递给程序。

您可以阅读this以了解将参数传递给main。

答案 2 :(得分:1)

loadFromFile函数定义中,您应该编写

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

您不应在函数定义中指定默认参数值。