类类型的重新定义 - c++

发布于 2024-09-30 12:18:09 字数 4127 浏览 7 评论 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

我知道我应该定义一次接口文件,但我不确定要更改什么,因为我尝试的所有操作都会带来更多问题。

我的驱动程序(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
}

我的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

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

/******************************************************************************
   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 语句有关,但我对要更改哪些语句感到困惑。如果有人能告诉我我做错了什么,我将非常感激。谢谢你!

I understand the problem I'm having with my code, but trying some of the things that others are suggesting isn't fixing my error.

Here's my error messages:

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

Here's the one I'm worried about:

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

I know I'm supposed to have my interface file defined once, but I am not sure what to change because everything I try is bringing more problems.

My driver(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);
}

My support file (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
}

My 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

And the header file - this one includes a lot of class functions I haven't done yet. It is unalterable.

/******************************************************************************
   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

I understand the problem has something to do with my #include statements, but I'm mixed up about which ones to change. If someone can show me what I'm doing wrong, I'd be very grateful. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅忆 2024-10-07 12:18:09
#include "proj07.string.cpp"

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

#include "proj07.string.cpp"

You need to include the header file, not the .cpp file. If you include the .cpp file, which presumably contains definitions in it, in multiple .cpp files, then when you build, you'll get multiple definition errors (because things are defined more than once in different .cpp files).

紅太極 2024-10-07 12:18:09

除了 James McNellis 所说的之外,您还定义了默认构造函数两个不同的地方:

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 的默认构造函数有两种不同的实现,这违反了单一定义规则。


一些与您的问题无关的评论:

我知道您无法更改project07.string.h,但它确实不应该有using namespace std > 就像里面那样。它使得任何包含 project07.string.h 的文件都会将整个 std 命名空间转储到其中,从而增加了您的标识符之一与其发生冲突的可能性。

此外,类声明似乎只需要 ostreamistream 的前向声明。在这种情况下,您只需要#include,而不是整个

In addition to what James McNellis said, you defined the default constructor in two different places:

In 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; 
    }
    /* ... */ 
};

And in proj07.support.cpp:

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

/*Default constructor*/

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

Since you included string.h within support.cpp, the compiler sees two different implementations of String's default constructor, which violates the One Definition Rule.


Some comments unrelated to your problem:

I know you can't alter project07.string.h, but it really should not have a using namespace std like that in it. It makes it so that any file that includes project07.string.h will have the entire std namespace dumped into it, increasing the probability that one of your identifiers will clash with it.

Also, it appears that the class declaration only needs a forward declaration of ostream and istream. In that case, you would only need to #include <iosfwd>, instead of the entire <iostream>.

拥抱我好吗 2024-10-07 12:18:09

正如消息所述,您的 String 构造函数定义了两次。一旦进入类定义:

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 否则您当前的 makefile 将出现链接器错误。

Your String constructor is, as the message states, defined twice. Once in the class definition:

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

and once in the string.cpp file, as such:

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

Also, you might want to include string.h in your main program, not main.cpp or you will get linker errors with your current makefile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文