尝试制作我自己的字符串类

发布于 2025-01-06 06:14:18 字数 2405 浏览 1 评论 0原文

这些是我的程序遇到的错误。

myString1.cpp: In constructor ‘MyString1::MyString1(char*, int)’:
myString1.cpp:6: error: expected primary-expression before ‘]’ token
myString1.cpp:6: error: expected primary-expression before ‘]’ token
myString1.cpp: In member function ‘MyString1 MyString1::append(MyString1)’: 
myString1.cpp:11: error: invalid use of member (did you forget the ‘&’ ?) 
myString1.cpp: In member function ‘void MyString1::clear()’:
myString1.cpp:25: error: expected primary-expression before ‘]’ token 
myString1.cpp:25: error: expected primary-expression before ‘{’ token
myString1.cpp:25: error: expected `;' before ‘{’ token 
myString1.cpp: In member function ‘bool MyString1::empty()’:
myString1.cpp:29: error: expected primary-expression before ‘]’ token
myString1.cpp:31: error: expected primary-expression before ‘else’ 
myString1.cpp:31: error: expected `;'  before ‘else’

这是我的三个不同部分的程序。

myString1.h

#ifndef MYSTRING1_H
#define MYSTRING1_H

class MyString1
{
  private:
   char chars[];
   int size;
  public:
   MyString1();
   MyString1(char chars[], int size);
   MyString1 append(MyString1 s);
   char at(int index);
   int length();
   void clear();
   bool empty();
   int find(char ch);
};
#endif

myString1.cpp

#include "myString1.h"
using namespace std;

MyString1::MyString1(char chars[], int size)
{
  this->chars[] = chars[];
  this->size = size;
}
MyString1 MyString1::append(MyString1 s)
{
  for(int i = size; i > size - s.length; i++)
    chars[i] = s.at(i);
}
char MyString1::at(int index)
{
  return chars[index];
}
int MyString1::length()
{
  return size;
}
void MyString1::clear()
{
  size = 0;
  chars[] = {};
}
bool MyString1::empty()
{
  if(chars[]){
    return true;
    else
      return false;
  }
}
int MyString1::find(char ch)
{
  for(int i = 0; i < size; i++)
    if(chars[i] = ch)
      return i;
}

testMyString1.cpp

#include <iostream>
#include "myString1.h"
using namespace std;

int main()
{
  MyString1 first("cat", 4);
  MyString1 second("dog", 4);
  cout << first.at(1) << " and " << second.at(1) << endl;
  first.append(second);
  cout << first.at(6) << endl;

  return 0;
}

我是一个新手,只是想学习如何使用 g++ 编译器,所以只是寻求一些帮助来阅读错误消息并调试我的代码。另外我确信有一些非常糟糕的代码,因此我们将不胜感激。

These are the errors I'm getting for my program.

myString1.cpp: In constructor ‘MyString1::MyString1(char*, int)’:
myString1.cpp:6: error: expected primary-expression before ‘]’ token
myString1.cpp:6: error: expected primary-expression before ‘]’ token
myString1.cpp: In member function ‘MyString1 MyString1::append(MyString1)’: 
myString1.cpp:11: error: invalid use of member (did you forget the ‘&’ ?) 
myString1.cpp: In member function ‘void MyString1::clear()’:
myString1.cpp:25: error: expected primary-expression before ‘]’ token 
myString1.cpp:25: error: expected primary-expression before ‘{’ token
myString1.cpp:25: error: expected `;' before ‘{’ token 
myString1.cpp: In member function ‘bool MyString1::empty()’:
myString1.cpp:29: error: expected primary-expression before ‘]’ token
myString1.cpp:31: error: expected primary-expression before ‘else’ 
myString1.cpp:31: error: expected `;'  before ‘else’

And here is my program in the three different parts.

myString1.h

#ifndef MYSTRING1_H
#define MYSTRING1_H

class MyString1
{
  private:
   char chars[];
   int size;
  public:
   MyString1();
   MyString1(char chars[], int size);
   MyString1 append(MyString1 s);
   char at(int index);
   int length();
   void clear();
   bool empty();
   int find(char ch);
};
#endif

myString1.cpp

#include "myString1.h"
using namespace std;

MyString1::MyString1(char chars[], int size)
{
  this->chars[] = chars[];
  this->size = size;
}
MyString1 MyString1::append(MyString1 s)
{
  for(int i = size; i > size - s.length; i++)
    chars[i] = s.at(i);
}
char MyString1::at(int index)
{
  return chars[index];
}
int MyString1::length()
{
  return size;
}
void MyString1::clear()
{
  size = 0;
  chars[] = {};
}
bool MyString1::empty()
{
  if(chars[]){
    return true;
    else
      return false;
  }
}
int MyString1::find(char ch)
{
  for(int i = 0; i < size; i++)
    if(chars[i] = ch)
      return i;
}

testMyString1.cpp

#include <iostream>
#include "myString1.h"
using namespace std;

int main()
{
  MyString1 first("cat", 4);
  MyString1 second("dog", 4);
  cout << first.at(1) << " and " << second.at(1) << endl;
  first.append(second);
  cout << first.at(6) << endl;

  return 0;
}

Im a newbie just trying to learn how to use the g++ compiler so just looking for some help reading the error messages and debugging my code. Also I'm sure there is some very bad code so any help is appreciated.

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

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

发布评论

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

评论(1

国产ˉ祖宗 2025-01-13 06:14:18

该代码有很多错误,所以我不知道从哪里开始,但我想通常给你一些指导来帮助你理解你的代码就可以了。

在我看来,您不需要在 String 类中拥有大小索引,因为有 strlen() 函数很乐意为您计算它。
现在,对于您的类声明,请检查如何声明将为您保存字符串的指针。您需要像下面这样做:

class MyString1
{
  private:
   char* chars;//this declares a pointer to a char that will hold the string for you
  public:
  ...

而且您永远不会分配保存字符串的 char* 。你的构造函数应该是:

MyString1::MyString1(const char* chars)
{
  this->chars = (char*) malloc(strlen(chars)+1); //this will allocate an array of strlen() chars +1
  strcpy(this->chars,chars);
}

正如你所看到的,我没有使用大小索引,因为 strlen 可以非常有效地为你找到它。 +1 代表“\0”,表示字符串结束。

现在要向字符串添加一些内容,这会很棘手。

void MyString1::append(const MyString1& s) //it's good to give a constant reference to the string here
{
  //first of all we gotta reallocate the pointer,since we don't have enough memory for the string
  int newsize = strlen(this->chars) + strlen(s)+1;
  this->chars = (char*) realloc(this->chars,newSize); \\ no check for realloc failure, I know but this is just an example

  strcat(this->chars,s.chars);

}

追加时不需要返回任何内容。你正在对这个字符串做一些事情。

你的 ::at() 函数几乎没问题。想象一下,如果字符串的大小为 10 并且您请求 MyString1::at(12) 会发生什么。这可能会导致分段错误(这不好)。

因此,您应该更改代码以进行如下所示的边界检查:

char MyString1::at(int index)
{
  //if it's out of bounds let's return -1 which will signify that we got an out of bounds value  (could also throw an exception here but that's a different subject altogether)
  if(index > strlen(this->chars) || index <0)
      return -1;

  return chars[index];
}

同样在 C/C++ 中,您必须释放分配的内存。因此,为了做到这一点,您应该声明一个称为析构函数的东西。

MyString1::~MyString1()
{
  free(this->chars);
}

最后,isempty 函数可以像这样:

bool MyString1::empty()
{
  return (this->chars[0] == '\0';
}

The code has a lot of mistakes so I don't know where to start from but I suppose that generally giving you some pointers to help you with understanding your code would be okay.

In my opinion you don't need to have a size index in a String class since there is the strlen() function that will gladly compute it for you.
Now for your class declaration check how you declare the pointer that will hold the string for you. You need to do it like below:

class MyString1
{
  private:
   char* chars;//this declares a pointer to a char that will hold the string for you
  public:
  ...

Also you are never allocating the char* that holds the strings. Your constructor should be:

MyString1::MyString1(const char* chars)
{
  this->chars = (char*) malloc(strlen(chars)+1); //this will allocate an array of strlen() chars +1
  strcpy(this->chars,chars);
}

As you can see I am not using a size index since strlen can quite efficiently find that out for you. +1 is for the '\0' that signifies the end of a string.

Now to append something to the string, that's gonna be tricky.

void MyString1::append(const MyString1& s) //it's good to give a constant reference to the string here
{
  //first of all we gotta reallocate the pointer,since we don't have enough memory for the string
  int newsize = strlen(this->chars) + strlen(s)+1;
  this->chars = (char*) realloc(this->chars,newSize); \\ no check for realloc failure, I know but this is just an example

  strcat(this->chars,s.chars);

}

You don't need to return anything when you append. You are doing something to THIS string.

Your ::at() function is almost okay. Imagine though what would happen if the size of the string was 10 and you request MyString1::at(12). This would probably cause a Segmentation fault (that's not good).

So you should alter your code to do bounds checking like below:

char MyString1::at(int index)
{
  //if it's out of bounds let's return -1 which will signify that we got an out of bounds value  (could also throw an exception here but that's a different subject altogether)
  if(index > strlen(this->chars) || index <0)
      return -1;

  return chars[index];
}

Also in C/C++ you have to free the memory that you allocate. So in order to do that you should declare something called a destructor

MyString1::~MyString1()
{
  free(this->chars);
}

Finally the is empty function can just be like that:

bool MyString1::empty()
{
  return (this->chars[0] == '\0';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文