首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

程序报错,请求帮忙看看,小弟我头疼了

2013-10-10 
程序报错,请求帮忙看看,我头疼了。首先我把错误的内容贴出来。1.obj : error LNK2001: unresolved external

程序报错,请求帮忙看看,我头疼了。
首先我把错误的内容贴出来。
1.obj : error LNK2001: unresolved external symbol "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class String &)" (??5@YAAAV?$basic_istream@DU?$char
_traits@D@std@@@std@@AAV01@AAVString@@@Z)
Debug/wuyong5.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.

wuyong5.exe - 1 error(s), 0 warning(s)

头文件内容
//.....................................................................................................................................
string1.h

#include <iostream>
using std::ostream;
using std::istream;
#ifndef STRING1_H_
#define STRING1_H_
class String
{
private:
char * str;             // pointer to string
int len;                // length of string
static int num_strings; // number of objects
//static  int CINLIM = 80;  // cin input limit
enum {CINLIM = 80};

public:
// constructors and other methods
String(const char * s); // constructor
String();               // default constructor
String(const String &); // copy constructor
~String();              // destructor
int length () const { return len; }
// overloaded operator methods    
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
// overloaded operator friends
friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st1, const String &st2);
friend bool operator==(const String &st, const String &st2);
friend ostream & operator<<(ostream & os, const String & st);
friend istream & operator>>(istream & is, String & st);
// static function
static int HowMany();
};
#endif


//.............................................................................................................................
string1.cpp


#include <cstring>                 // string.h for some
#include "string1.h"               // includes <iostream>
// using cin;
// using cout;
// initializing static class member
int String::num_strings = 0;
// static method
int String::HowMany()
{
return num_strings;  //这是一个静态变量,计数。看有多少对象调用他了、
}
// class methods
String::String(const char * s)     // construct String from C string
{
len = strlen(s);          // 设置传入的字符长度。
str = new char[len + 1];       // 分配新的空间,空间长度为传进来的字符长度+1.为什么+1?
strcpy(str, s);           // 拷贝字符串地址到空间。
num_strings++;                 //计数
}
String::String()                   // 默认的构造函数,不带参数
{
len = 4;
str = new char[1];
str[0] = '\0';                 // default string
num_strings++;
}
String::String(const String & st)
{
num_strings++;             // handle static member update
len = st.len;              // same length
str = new char [len + 1];  // allot space
strcpy(str, st.str);  // copy string to new location
}
String::~String()                     // necessary destructor
{
--num_strings;                    // required
delete [] str;                    // required


}
// overloaded operator methods    
// assign a String to a String
String & String::operator=(const String & st)
{
if (this == &st)
return *this;
delete [] str;
len = st.len;
str = new char[len + 1];
    strcpy(str, st.str);
return *this;
}
// assign a C string to a String
String & String::operator=(const char * s)
{
delete [] str;
len = strlen(s);
str = new char[len + 1];
    strcpy(str, s);
return *this;
}
// read-write char access for non-const String
char & String::operator[](int i)
{
return str[i];
}
// read-only char access for const String
const char & String::operator[](int i) const
{
return str[i];
}
// overloaded operator friends
bool operator<(const String &st1, const String &st2)
{
return (strcmp(st1.str, st2.str) < 0);
}
bool operator>(const String &st1, const String &st2)
{
return st2.str < st1.str;
}
bool operator==(const String &st1, const String &st2)
{
return (strcmp(st1.str, st2.str) == 0);
}
// simple String output
ostream & operator<<(ostream & os, const String & st)
{
os << st.str;
return os; 
}


//.............................................................................................................................

#include <iostream>
#include <cstdlib>      // (or stdlib.h) for rand(), srand()
#include <ctime>        // (or time.h) for time()
#include "string1.h"
const int ArSize = 10;
const int MaxLen = 81;
using namespace std;
int main()
{

String name;
cout <<"Hi, what's your name?\n>> ";
cin>>name;
cout << name << ", please enter up to " << ArSize
<< " short sayings <empty line to quit>:\n";
String sayings[ArSize];
char temp[MaxLen];               // temporary string storage
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') // empty line?
break;                   // i not incremented
else
sayings[i] = temp;       // overloaded assignment
}
int total = i;                   // total # of lines read
if (total > 0)
{
cout << "Here are your sayings:\n";
for (i = 0; i < total; i++)
cout << sayings[i] << "\n";
// use pointers to keep track of shortest, first strings
String * shortest = &sayings[0]; // initialize to first object
String * first = &sayings[0];
for (i = 1; i < total; i++)
{
if (sayings[i].length() < shortest->length())
shortest = &sayings[i];
if (sayings[i] < *first)     // compare values
first = &sayings[i];     // assign address
}
cout << "Shortest saying:\n" << * shortest << endl;
cout << "First alphabetically:\n" << * first << endl;
srand(time(0));
int choice = rand() % total;     // pick index at random
// use new to create, initialize new String object
String * favorite = new String(sayings[choice]);
cout << "My favorite saying:\n" << *favorite << endl;
delete favorite;
}
else
cout << "Not much to say, eh?\n";
cout << "Bye.\n";
return 0; 
}


//................................................................................................................................


测试后是cin>>name;的问题可能。。求解。
谢谢。
不要嫌弃分少,。感谢你。
[解决办法]
friend istream & operator>>(istream & is, String & st);
没定义吧

热点排行