0

コードで発生している問題は理解していますが、他の人が提案しているいくつかのことを試してもエラーが修正されません。

エラーメッセージは次のとおりです。

In file included from proj07.driver.cpp:4:
proj07.string.cpp:10: error: redefinition of 'String::String()'
/user/cse232/Projects/project07.string.h:25: error: 'String::String()' previously defined here
proj07.string.cpp: In constructor 'String::String(const char*)':
proj07.string.cpp:19: error: expected primary-expression before 'char'
proj07.string.cpp:19: error: expected `;' before 'char'
make: *** [proj07.driver.o] Error 1

ここで私が心配しているのは次のとおりです。

proj07.string.cpp:10: error: redefinition of 'String::String()'
/user/cse232/Projects/project07.string.h:25: error: 'String::String()' previously defined here

インターフェイス ファイルを 1 回定義する必要があることはわかっていますが、何を変更しようとしても問題が増えるため、何を変更すればよいかわかりません。

私のドライバー (proj07.driver.cpp):

using namespace std;

#include <iostream>
#include "proj07.string.cpp"

int main()
{
  const char string1[] = {'a', 'b', 'c', 'd', 'e', 'f'};
  String test1();
  String test2(string1);
}

私のサポート ファイル (proj07.support.cpp):

/* Implementation file for type "String" */

using namespace std;

#include <iostream>
#include "/user/cse232/Projects/project07.string.h"

/*Default constructor*/

String::String()
{
  Capacity = 0;
  Length = 0;
  Mem = NULL;
}

String::String( const char[] )
{
  cout << char.length[]; //Function not implemented yet
}

私のメイクファイル:

#
# Project 07
#

proj07.exe: proj07.driver.o proj07.string.o
        g++ -o proj07.exe proj.driver. proj07.string.o

proj07.driver.o: proj07.driver.cpp ~cse232/Projects/project07.string.h
        g++ -Wall -c proj07.driver.cpp

proj07.string.o: proj07.string.cpp ~cse232/Projects/project07.string.h
        g++ -Wall -c proj07.string.cpp

clean:
        rm -f proj07.*.o proj07.exe

そしてヘッダー ファイル - これには、まだ行っていない多くのクラス関数が含まれています。それは不変です。

/******************************************************************************
   Project #7 -- Interface file for type "String"
******************************************************************************/

#ifndef STRING_
#define STRING_

using namespace std;

#include <iostream>

class String
{
  private:

    unsigned Capacity;  // Number of memory locations reserved
    unsigned Length;    // Number of memory locations in use
    char * Mem;         // Pointer to memory to hold characters

  public:

    // Construct empty string
    //
    String()
    {
      Capacity = 0;
      Length = 0;
      Mem = NULL;
    }

    // Reset string to empty
    //
    void reset() { Length = 0; }

    // Return string capacity and length
    //
    unsigned capacity() const { return Capacity; }
    unsigned length() const { return Length; }

    // Return string status
    //
    bool empty() const { return Length == 0; }

    // Return reference to element I
    //
    char& operator[]( unsigned I ) { return Mem[I]; }

    // Return constant reference to element I
    //
    const char& operator[]( unsigned I ) const { return Mem[I]; }

    // Destroy string
    //
    ~String();

    // Construct string by copying existing string
    //
    String( const String& );

    // Construct string by copying C-style character string
    //
    String( const char[] );

    // Copy string into the current string
    //
    String& operator=( const String& );                                 

    // Append string to the current string
    //
    String& operator+=( const String& );
};

// Return string which is the concatenation of two strings
//
String operator+( const String&, const String& );

// Compare two strings (equality and relational operators)
//
bool operator==( const String&, const String& );
bool operator< ( const String&, const String& );

// Output string to stream
//
ostream& operator<<( ostream&, const String& );

// Input string from stream
//
istream& operator>>( istream&, String& );

#endif

問題が #include ステートメントに関係していることは理解していますが、どのステートメントを変更するかについて混乱しています。誰かが私が間違っていることを教えてくれたら、とても感謝しています。ありがとうございました!

4

3 に答える 3

6
#include "proj07.string.cpp"

.cpp ファイルではなく、ヘッダー ファイルを含める必要があります。おそらく定義が含まれている .cpp ファイルを複数の .cpp ファイルに含めると、ビルド時に複数の定義エラーが発生します (異なる .cpp ファイルで複数回定義されているため)。

于 2010-11-03T21:50:43.717 に答える
4

James McNellisが言ったことに加えて、デフォルトのコンストラクターを 2 つの異なる場所で定義しました。

project07.string.h

class String 
{ 
  private: 

    unsigned Capacity;  // Number of memory locations reserved 
    unsigned Length;    // Number of memory locations in use 
    char * Mem;         // Pointer to memory to hold characters 

  public: 

    // Construct empty string 
    // 
    String() 
    { 
      Capacity = 0; 
      Length = 0; 
      Mem = NULL; 
    }
    /* ... */ 
};

そしてでproj07.support.cpp

#include "/user/cse232/Projects/project07.string.h" 

/*Default constructor*/

String::String() 
{ 
  Capacity = 0; 
  Length = 0; 
  Mem = NULL; 
} 

string.h内に含めたためsupport.cpp、コンパイラはStringのデフォルト コンストラクタの 2 つの異なる実装を認識します。これは、1 つの定義規則に違反しています。


問題に関係のないコメント:

変更できないことはわかっていますがproject07.string.h、実際にはそのusing namespace stdようなものを含めるべきではありません。インクルードするファイルには名前空間project07.string.h全体がstdダンプされるため、識別子の 1 つが衝突する可能性が高くなります。

また、クラス宣言にはostreamandの前方宣言のみが必要なようですistream。その場合、#include <iosfwd>全体ではなく、のみが必要になります<iostream>

于 2010-11-03T21:51:46.563 に答える
1

Stringメッセージが示すように、コンストラクターは2回定義されています。クラス定義に入ったら:

class String {
    // Construct empty string
    //
    String()
    {
         Capacity = 0;
         Length = 0;
         Mem = NULL;
    }
};

string.cppファイルに一度、次のように入力します。

String::String()
{
    Capacity = 0;
    Length = 0;
    Mem = NULL;
}

また、現在のメイクファイルでリンカ エラーが発生しstring.hないように、メイン プログラムに含めたい場合もあります。main.cpp

于 2010-11-03T21:52:27.987 に答える