重新定义类类型 - c ++

Meg*_*ose 0 c++ class redefinition

我理解我的代码所遇到的问题,但尝试其他人建议的一些事情并不能解决我的错误.

这是我的错误消息:

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
Run Code Online (Sandbox Code Playgroud)

这是我担心的那个:

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

我知道我应该定义一次我的接口文件,但我不确定要改变什么,因为我尝试的一切都带来了更多的问题.

我的驱动程序(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);
}
Run Code Online (Sandbox Code Playgroud)

我的支持文件(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
}
Run Code Online (Sandbox Code Playgroud)

我的makefile:

#
# 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
Run Code Online (Sandbox Code Playgroud)

头文件 - 这个包含了许多我还没有完成的类函数.这是不可改变的.

/******************************************************************************
   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
Run Code Online (Sandbox Code Playgroud)

我知道这个问题与我的#include语句有关,但是我想知道哪些问题要改变.如果有人能告诉我我做错了什么,我将非常感激.谢谢!

Jam*_*lis 6

#include "proj07.string.cpp"
Run Code Online (Sandbox Code Playgroud)

您需要包含头文件,而不是.cpp文件.如果在.cpp文件中包含.cpp文件(可能包含其中的定义),那么在构建时,您将获得多个定义错误(因为在不同的.cpp文件中定义了多次).