【问题标题】:error with string in dynamic memory c++ program动态内存c ++程序中的字符串错误
【发布时间】:2013-07-23 21:42:17
【问题描述】:

过去一小时我一直在尝试编译这个程序,但没有成功。 我不知道出了什么问题,但是,我认为构造函数有问题。 整个文件太长无法打印,所以我只打印一个 sn-p。

String1.h

#ifndef STRING1_H
#define STRING1_H
#include <iostream>
using std::ostream;
using std::istream;


class String {
    char *str;
    int len;
    static int num_strings;
    static const int CINLIM = 80;
public:

String(const char *s);
String();
String(const String &); //COPY CONSTRUCTOR: PASS-BY-REF ALWAYS
 ~String();
 int length() const {
    return len;
}
 String & operator=(const String &);
String & operator=(const char* );
 char & operator [](int i);
const char & operator [](int i) const;
friend std::istream &operator>>(std::istream & is, String & st);
friend std::ostream &operator<<(std::ostream & os, const String & st);
 static int HowMany();
};

String.cpp

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


int String::num_strings = 0;

int String::HowMany() {
    return num_strings;
}


String::String(const char* s) {
    len = std::strlen(s);
    str = new char [len + 1];
    std::strcpy(str, s);
    num_strings++;
} 


String::String() {
    len = 4;
    str = new char[1];
   std::strcpy(str, "C++");
    str[0] = '\0';
    num_strings++;
}


String::String(const String & st) {
    num_strings++;
    len = st.len;
    str = new char[len + 1];
    std::strcpy(str, st.str);

}

String::~String() {

    --num_strings;
    delete [] str;
}

String & String::operator =(const String& st) {
    if (this == &st)
        return *this;
    delete [] str;
    len = st.len;
    str = new char [len + 1];
    std::strcpy(str, st.str);
    return *this;
}


String &String::operator=(const char* s) {
    delete[]str;
    len = std::strlen(s);
    str = new char [len + 1];
    std::strcpy(str, s);
    return *this;
} 


char & String::operator [](int i) {
    return str[i];
}

const char & String::operator [](int i) const {
    return str[i];
}


ostream & operator <<(ostream& os, const String& st) {

    os << st.str;
    return os;
}

istream & operator >>(iostream & is, String &st) {
    char temp[String::CINLIM];
    is.get(temp,String::CINLIM);
    if(is)
        st=temp;
    while(is&&is.get()!='\n')
        continue;
    return is;
}

ma​​in.cpp

#include <iostream>

#include "String1.h"
using namespace std;

const int ArSize = 10;
const int MaxLen = 81;

int main() {
    String name;
   cout << "Hi what is your name?\n";
   cin >> name;
   cout << "Please enter up to " << ArSize <<
            "sayings <empty line to quit>:\n";

   String sayings[ArSize];
 char temp [MaxLen];
    int i;

    for (i = 0; i < ArSize; i++){
        cout << i + 1 << ":";
    cin.get(temp, MaxLen);

    while (cin && cin.get() != '\n')
        continue;
    if (!cin || temp[0] == '\0')
        break;
    else
        sayings[i] = temp;
  }
    int total = i;
}

我得到的错误信息是:

    /usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/string
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/main.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/main.o.d -o build/Debug/GNU-MacOSX/main.o main.cpp
mkdir -p dist/Debug/GNU-MacOSX
g++     -o dist/Debug/GNU-MacOSX/string build/Debug/GNU-MacOSX/main.o  
Undefined symbols for architecture x86_64:
  "String::String()", referenced from:
      _main in main.o 
  "operator>>(std::basic_istream<char, std::char_traits<char> >&, String&)", referenced from:
      _main in main.o
  "String::~String()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-MacOSX/string] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

请有人告诉我这里出了什么问题。如果您需要我发布整个程序,而不是 sn-p,我将很乐意这样做。

【问题讨论】:

标签: c++ string constructor dynamic-memory-allocation


【解决方案1】:

首先,默认情况下,类访问权限是privateString::CINLIM 是私有的,你不能直接访问它。您需要授予对 CINLIM 的公共访问权限或将其移出 String 类。

char temp[String::CINLIM];   // Error, String::CINLIM is private
is.get(temp,String::CINLIM); // same Error

class String {
    char *str;
    int len;
    static int num_strings;
public:
    static const int CINLIM = 80;  // make CINLIM public. 
                                   // Now you can access String::CINLIM directly
//....
};

第二个问题,你声明了

std::istream &operator>>(std::istream & is, String & st);
//                            ^^^^^^^^

但你的实现是:

operator >>(iostream & is, String &st)
//          ^^^^^^^^

评论更新:

你需要在g++命令中链接String.o

g++ -o dist/Debug/GNU-MacOSX/string \
build/Debug/GNU-MacOSX/main.o build/Debug/GNU-MacOSX/String.o

【讨论】:

  • @jis 没错。您的代码很长,我并没有完全准确。
  • 我将String::CINLIM 移到了头文件的公共部分,但仍然出现错误。你知道这是为什么吗?
  • 嗯,现在的错误信息是什么?它在我的 Linux 上干净地构建
  • Undefined symbols for architecture x86_64: "String::String()", referenced from: _main in main.o "operator&gt;&gt;(std::basic_istream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, String&amp;)", referenced from: _main in main.o "String::~String()", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status
  • 你只需要链接String.o
【解决方案2】:

您在“String1.cpp”中定义了函数:

istream & operator >>(iostream & is, String &st)

而不是

istream & operator >>(istream & is, String &st)

【讨论】:

  • 实际上,我可以看到我在 cpp 文件中有一个拼写错误:std::istream &operator>>(std::istream & is, String & st);。我已经解决了这个问题,但它没有任何区别。
  • 在 vs2012 中构建良好
猜你喜欢
  • 1970-01-01
  • 2015-12-13
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 2019-05-15
  • 2018-05-18
  • 1970-01-01
相关资源
最近更新 更多