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

标签 c++ class compilation g++

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

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

我的String1.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++ 编译器,所以只是寻求一些帮助来阅读错误消息和调试我的代码。此外,我确定有一些非常糟糕的代码,因此我们将不胜感激。

最佳答案

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

在我看来,您不需要在 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);
}

最后 is empty 函数可以像这样:

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

关于c++ - 尝试制作我自己的字符串类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9322936/

相关文章:

java - 我的 Java 程序无法编译

c++ - Bazel 允许我包含来自全局安装的库的 header

c++ - g++ - 为什么在使用 std::thread 时必须传递 "-pthread"选项?

c++ - 尽管创建了默认构造函数,但没有合适的默认构造函数可用?

html - animate.css 仅在您的屏幕看到元素 ID 时生效

linux - 从源代码自定义编译 Apache Server

makefile - 子项目的主 Makefile 不会编译子项目

c++ - include_directories 无法正常工作

c++ - 静态局部变量中的竞争条件

c++ - 我的课干扰了图书馆的课