2016-04-11 6 views
1

以下は私のコードですし、その下にエラーメッセージです。助けていただければ幸いです。ありがとうございました。割り当ては以下の通りである。C++ Pascal Stringsエラーです。私のコードでエラーを見つけることができません

なしで、これらの文字が続く文字列の文字数、で始まる文字配列されている内部店パスカル文字列に提供Stringクラスを変更しますヌル文字を終了します。つまり、

  • strにはパスカル文字列を含める必要があります。
  • パスカル文字列ラボで作成されたコンストラクタを含める必要があります。 通常のC文字列コンストラクタもインクルードする必要があり、パスカル文字列に変換する必要があります。
  • c_str関数は、ユーザーにC文字列を提供するためにC文字列に変換する必要があります。この関数では文字列のメモリを割り当てても構いません。
  • 内部文字列形式を変更すると、他のすべての関数は正しく機能する必要があります。
  • でない可能性があります。は、パスカル文字列とともに内部的にC文字列を格納します。
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)" ([email protected]@YAHXZ) Pascal Assignment C:\Users\Danielle\Documents\Visual Studio 2015\Projects\Pascal Assignment\MSVCRTD.lib(exe_main.obj) 1 
+1

私は読めないです愛の画像を行います。関連するテキストをcut'n'pasteしてください –

+0

私はそれを見ることができますか?画像をクリックすると、より大きなバージョンが表示されます。もう一度やり直すことができます。 – danie

+0

あなたのアプリケーションを実行するために必要な主な機能はありません。私はmain.cppを作成することをお勧めします[main function](http://stackoverflow.com/a/4207223/332733) – Mgetz

答えて

2

mainの機能が定義されておらず、プログラムとしてコンパイルしているためです。どちらかがライブラリプロジェクトであるか、またはmain機能が必要です。 コードの末尾にある#endifは、これがヘッダーであり、ヘッダーの先頭が表示されていないことを示しています。そうならば、Yはあなたには、あなたのmainを置くために(例えば)別の.cppファイルを必要とする他のニュースで


:。__MYSTRING_H__のような二つの連続アンダースコアが含まれている

  • 識別子、は、が実装されているため、が予約されているため、トラブルが発生する可能性があります。また、アンダースコアと大文字で始まる識別子は予約されています。例えばただMYSTRING_H。しかし、

  • #pragma onceを使用する場合は、ガードシンボルを含める必要はありません。

  • 私は本当にコードを勉強していないが、あなたは<<のような操作を使用しない限り、または例えばendlのヘッダーには、<iostream>をすべてヘッダーに含める必要はありません。 <iosfwd>を含めることで十分です。それはそれのためのものです:痩せた&は関連する型の宣言だけを意味します。標準ストリーム(つまり実装ファイル)を使用する場合は、<iostream>を追加する必要があります。しかし、このコストはクライアントコードに課金されません。

+0

私はmain.cppファイルを持っていました。私はヘッダファイルをインクルードするだけで何も持っていなかったので表示していませんでした。 – danie

+0

さて、できるだけ短い 'main'を' int main(){} 'に追加してください。 –

+0

この回答では、ストライクアウトされたテキストがあります。コードを勉強した結果、実装ファイルとヘッダーの両方が連結されていることがわかりました。質問を編集してファイル名のヘッダーを追加しました。その後、ストライクアウトを追加します。 –

関連する問題