0

ここで本当に簡単な質問です。仮想関数を使用してテキストファイルから読み込みています。ある面では値を正規化する必要があり、別の面では値を正規化したくないので、これは仮想です。私はこれをやろうとしました:

bool readwav(string theFile, 'native');

したがって、理論的には、「native」を使用する場合はこのメソッドを呼び出す必要がありますが、「double」を呼び出す場合は、別のバージョンのメソッドが呼び出されます。値が空の場合も同様で、ネイティブオプションを実行するだけです。

最初の質問、なぜ上記の宣言が機能しないのですか?また、これは降りるのに最適なルートですか?または、オプションを切り替えるクラスメソッドを1つだけ持つ方がよいでしょうか。

ありがとう :)

アップデート:

どこが間違っているのですか?

bool Wav::readwav(string theFile, ReadType type = NATIVE)
{
// Attempt to open the .wav file
ifstream file (theFile.c_str());

if(!this->readHeader(file))
{
    cerr << "Cannot read header file";
    return 0;
}

for(unsigned i=0; (i < this->dataSize); i++)
{
    float c = (unsigned)(unsigned char)data[i];

    this->rawData.push_back(c);
}

return true;
}

bool Wav::readwav(string theFile, ReadType type = DOUBLE)
{
  // Attempt to open the .wav file
  ifstream file (theFile.c_str());

  cout << "This is the double information";
  return true;
 }
4

3 に答える 3

3

'native'は複数文字であり、文字列ではないためです。ただし、関数の複数のバージョンを使用します。

bool readwavNative(string theFile);
bool readwavDouble(string theFile);

または少なくともenum2番目のパラメータとして:

enum ReadType
{
   ReadNative,
   ReadDouble
};

//...
bool readwav(string theFile, ReadType type);
于 2012-09-10T09:35:34.073 に答える
1

必要なのは、デフォルトのパラメーターを使用した列挙であるように聞こえます。

enum FileType
{
  NATIVE=0,
  DOUBLE      
};

bool readwav(string theFile, FileType type = NATIVE);

デフォルトのパラメータは関数宣言にあります。定義には入れないでください。

bool readwav(string theFile, FileType type)
{
  switch(type)
  {
    case NATIVE: { ... } break;
    case DOUBLE: { ... } break;
    default: { ... } break;
  }
}

readwavこのように、パラメータなしで呼び出すとNATIVE、デフォルトで型が使用されます。

readwav("myfile.wav"); // Uses NATIVE type
readwav("myfile.wav", NATIVE); // Also uses NATIVE
readwav("myfile.wav", DOUBLE); // Uses DOUBLE type
于 2012-09-10T09:37:35.263 に答える
1

質問にはおっとが含まれているので、おっと答えが必要だと思います。戦略パターンはあなたの目的に合っていると思います。

class WavReader
{
public:
    WavReader(const std::string fileName)
    {
        //open file and prepare to read
    }

    virtual ~WavReader()
    {
        //close file
    }

    virtual bool read()=0;
};

class NativeWavReader: public WavReader
{
public:
    NativeWavReader(const std::string fileName): WavReader(fileName){}

    virtual bool read()
    {
        //native reading method
        std::cout<<"reading\n";
        return true;
    }
};

NativeWavReaderreadストラテジーのメソッドを実装します。別のメソッドが必要な場合は、ファイルを別の方法で読み取るWavReaderクラスを作成します。OtherWavReader

于 2012-09-10T09:51:13.713 に答える