C ++ Pascal Strings错误。我在代码中找不到错误

时间:2016-04-11 23:41:06

标签: c++ string class

下面是我的代码,然后是错误消息。我将不胜感激任何帮助。谢谢。作业如下:

  

修改提供的String类以在内部存储Pascal字符串,字符串数组以字符串中的多个字符开头,后跟这些字符,没有终止空字符。那就是:

     
      
  • str应包含Pascal字符串。
  •   
  • 应包含在Pascal字符串实验室中创建的构造函数。   还应包含普通的C字符串构造函数,并且必须转换为Pascal字符串。
  •   
  • c_str函数必须转换回C字符串,以便为用户提供C字符串。可以在此函数中为字符串分配内存。
  •   
  • 如果更改内部字符串格式,则所有其他功能必须正确执行。
  •   
  • 您可能在内部存储C字符串以及Pascal字符串。
  •   
Pascal.cpp
// Pascal Main cpp


#include <iostream>
#include <algorithm>
#include "Pascal.h"
#include <exception>
using namespace std;
// Default constructor
String::String() {
    arr = new char[1];
    arr[0] = '\0';
    len = 0;
}

// Constructor. Converts a C-string to a String object
String::String(const char *s) {
    len = strlen(s);
    arr = new char[len + 1];
    std::copy(s, s + len + 1, arr);
}

// Copy constructor.
String::String(const String &obj) {
    len = obj.len;
    arr = new char[len + 1];
    std::copy(obj.arr, obj.arr + len + 1, arr);
}

// Move constructor.
String::String(String &&obj) {
    len = obj.len;
    arr = obj.arr;
    obj.arr = nullptr;
}

String::String(const char *str, bool pascal) {
    judge = pascal;
    if (judge) {
        len = strlen(str) - 1;
        const char *temp = str;
        arr = new char[len + 1];
        arr[0] = len + '0';
        for (int i = 1; i <= len; i++) {
            arr[i] = temp[i];
        }

    }
    else {
        len = strlen(str);
        arr = new char[len + 1];
        std::copy(str, str + len + 1, arr);
    }
}

// Destructor
String::~String() {
    if (arr != nullptr)
        delete[] arr;
}

// Assignment operator
String &String::operator=(const String &rhs) {
    delete[] arr;
    len = rhs.len;
    arr = new char[len + 1];
    std::copy(rhs.arr, rhs.arr + len + 1, arr);
    return *this;
}

// Move assignment operator
String &String::operator=(String &&rhs) {
    delete[] arr;
    len = rhs.len;
    arr = rhs.arr;
    rhs.arr = nullptr;
    return *this;
}


// Mutator operator[]
char &String::operator[](int index) {
    // check whether the index is within bounds
    if (index > len || index < 0)
        throw std::out_of_range("Index out of range");
    return arr[index];
}

// Accessor operator[]
char String::operator[](int index) const {
    // check whether the index is within bounds
    if (index > len || index < 0)
        throw std::out_of_range("Index out of range");
    return arr[index];
}

// Get the length (number of characters) of a String object
int String::length() const {
    return len;
}

bool operator==(const String &lhs, const String &rhs) {
    if (lhs.judge != rhs.judge) {
        cout << "can't compare";
    }
    return strcmp(lhs.arr, rhs.arr) == 0;
}

bool operator<(const String &lhs, const String &rhs) {
    if (lhs.judge != rhs.judge) {
        cout << "can't compare";
    }
    return strcmp(lhs.arr, rhs.arr) < 0;
}

// Friend functions for > comparison
bool operator>(const String &lhs, const String &rhs) {
    if (lhs.judge != rhs.judge) {
        cout << "can't compare";
    }
    return rhs < lhs;
}

// Friend functions for <= comparison
bool operator<=(const String &lhs, const String &rhs) {
    if (lhs.judge != rhs.judge) {
        cout << "can't compare";
    }
    return !(rhs < lhs);
}

// Friend functions for >= comparison
bool operator>=(const String &lhs, const String &rhs) {
    if (lhs.judge != rhs.judge) {
        cout << "can't compare";
    }
    return !(lhs  < rhs);
}

// Friend functions for != comparison
bool operator!=(const String &lhs, const String &rhs) {
    if (lhs.judge != rhs.judge) {
        cout << "can't compare";
    }
    return !(lhs == rhs);
}

//  Friend function for string concatination
String operator+(const String &lhs, const String &rhs) {
    if (lhs.judge == rhs.judge && lhs.judge == false) {
        int strLength = lhs.len + rhs.len + 1;
        char *tmpStr = new char[strLength];
        for (auto i = 0; i < lhs.len; ++i)
            tmpStr[i] = lhs.arr[i];
        for (auto i = 0; i <= rhs.len; ++i)
            tmpStr[lhs.len + i] = rhs.arr[i];
        String retStr(tmpStr);
        delete[] tmpStr;
        return retStr;
    }
    else if (lhs.judge == rhs.judge && lhs.judge == true) {
        int strLength = lhs.len + rhs.len + 1;
        char *tmp = new char[strLength];
        for (auto i = 1; i <= lhs.len; ++i)
            tmp[i] = lhs.arr[i];
        for (auto i = 1; i <= rhs.len; ++i)
            tmp[lhs.len + i] = rhs.arr[i];
        tmp[0] = (lhs.len + rhs.len) + '0';
        String retStr(tmp);
        delete[] tmp;
        return retStr;
    }
    else {
        return String("can't do that");
    }
}

// Return C style character string
const char* String::c_str() const {
    return arr;
}

//  Friend function for output
std::ostream& operator<<(std::ostream &out, const String &obj) {
    return out << obj.c_str();
}
Pascal.h
// Pascal Header File

#pragma once
#ifndef __MYSTRING_H__
#define __MYSTRING_H__

#include <iostream>

class String {
public:
    // Usage: String aStringObj; or String aStringObj();
    String();

    // Constructor. Converts a char* object to a String  object
    // Usage: String aStringObj("hello"); or String aStringObj = "hello";
    String(const char *s);

    // Copy and move constructors.
    // Usage: String aStringObject(anotherStringObj); or
    // String aStringObject = anotherStringObj;
    String(const String &s);
    String(const char *str, bool pascal);
    String(String&& obj);

    // Destructor
    ~String();

    // Assignment operator
    // Usage: aStringObject = anotherStringObj; or
    // aStringObject.operator=(anotherStringObj);
    String &operator=(const String &rhsObject);
    String& operator=(String&& rhs);

    // Mutator operator[]
    // Usage: aStringObject[1] = ’M’;
    char &operator[] (int index);

    // Accessor operator[]
    // Usage: char ch = aStringObject[1];
    char operator[] (int index) const;

    // Get the length (number of characters) of a String object
    // Usage: int len = aStringObject.Length();
    int length() const;

    // Friend functions for == comparison
    // Usage: if (aStringObject == anotherStringObj) {...} or
    //        if (aStringObject == "hello") {...} or
    //        if ("hello" == aStringObj) {...} or
    friend bool operator==(const String &lhsObject, const String &rhsObject);

    // The other five comparison operators
    // !=, <, >, <=, >= are similarly handled as in line 13.

    friend bool operator<(const String &lhsObject, const String &rhsObject);
    friend bool operator>(const String &lhsObject, const String &rhsObject);
    friend bool operator<=(const String &lhsObject, const String &rhsObject);
    friend bool operator>=(const String &lhsObject, const String &rhsObject);
    friend bool operator!=(const String &lhsObject, const String &rhsObject);

    // Friend function for string concatenation
    // Usage: StringOne = StringTwo + StringThree  or
    //        StringOne = "hello" + StringTwo  or
    //        StringOne = StringTwo + "hello"
    friend String operator+(const String &lhs, const String &rhs);

    // Return C style character string
    // Usage: const char *str = aStringObj.C_str();
    const char *c_str() const;

    // Friend function for output
    // Usage: cout << aStringObj;
    friend std::ostream &operator<<(std::ostream &out, const String &obj);

private:
    // arr implements the String object as a dynamic array
    char *arr;

    // len keeps track of the length
    int len;

    // judge weather the String is a c_string or a pascal_string
    bool judge;
};

#endif

编译时我收到这些警告和错误:

enter image description here

Severity    Code    Description Project File    Line    Suppression State
Warning C4996   'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'    Pascal Assignment   c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility 2229    

 Severity   Code    Description Project File    Line    Suppression State
Error   LNK1120 1 unresolved externals  Pascal Assignment   C:\Users\Danielle\Documents\Visual Studio 2015\Projects\Pascal Assignment\Debug\Pascal Assignment.exe   1   

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)   Pascal Assignment   C:\Users\Danielle\Documents\Visual Studio 2015\Projects\Pascal Assignment\MSVCRTD.lib(exe_main.obj) 1   

1 个答案:

答案 0 :(得分:2)

缺少 main 功能是因为您尚未定义一个,并且您正在编译为程序。这应该是一个库项目,或者应该有一个main函数。 代码末尾的#endif表示这是一个标题,其中没有显示它的开头。如果是,则y 您需要一个单独的.cpp文件(例如)来放置main

在其他新闻中:

  • 包含两个连续下划线的标识符(如 __MYSTRING_H__ )对实现保留,因此可能会造成麻烦。此外,保留以下划线开头,后跟大写的标识符。使用例如只是MYSTRING_H。但是:

  • 当您使用#pragma once时,您不需要包含保护符号。

  • 我还没有真正研究过代码,但除非您使用<<之类的操作,或者例如endl,在标题中,您不需要在标题中包含整个<iostream>。它包含<iosfwd>就足够了。它的用途是什么:精益和精益仅指相关类型的声明。您仍然需要在使用标准流的地方包含<iostream>,即在您的实施文件中。但是这笔费用不计入客户代码。