我不确定为什么我一直收到这个错误。我实际上是根据我的朋友检查我的代码,但我无法理解为什么我的代码在他的代码中没有收到错误。尽管错误消息几乎是不可读的,但似乎它在process.h,我的显示功能或我的<<<<<<<<<<<<<<操作
// cstring.h
#define _CRT_SECURE_NO_WARNINGS
#ifndef _CSTRING_H_
#define _CSTRING_H_
#include <iostream>
#include <cstring>
#define MAX 5
namespace w1 {
class Cstring {
char* stored;
public:
Cstring();
Cstring(char* str);
void display(std::ostream& os) const;
};
}
std::ostream& operator<<(std::ostream& os, const w1::Cstring& ss);
#endif
// cstring.cpp
#include "cstring.h"
#include <iostream>
#define _CRT_SECURE_NO_WARNINGS
using w1::Cstring;
int max = MAX;
namespace w1{
Cstring::Cstring() {
stored = nullptr;
}
Cstring::Cstring(char* s) {
if (s != nullptr) {
stored = new char[MAX + 1];
strncpy(stored, s, MAX);
}
else {
*this = Cstring();
}
}
void Cstring::display(std::ostream& os) const {
static int counting = 0;
os << counting++ << ": " << stored << std::endl;
}
std::ostream& operator<<(std::ostream& os, const w1::Cstring& ss) {
ss.display(os);
return os;
}
}
// process.h
#include "cstring.h"
void process(char* st);
// process.cpp
#include "process.h"
#include "cstring.h"
void process(char* st) {
w1::Cstring first(st);
std::cout << first << std::endl;
}
// main.cpp中
#include "cstring.h"
#include "process.h"
int main(int argc, char *argv[]) {
int i;
std::cout << "Command Line: ";
for (i = 0; i < argc; i++) {
std::cout << argv[i] << " ";
}
std::cout << std::endl;
if (argc > 1) {
std::cout << "Maximum number of characters stored: " << MAX << std::endl;
for (int n = 1; n < argc; n++) {
process(argv[i]);
}
}
else {
std::cerr << "Insufficient number of arguments (min 1)" << std::endl;
}
}
错误消息是这样的:
错误1错误LNK2019:未解析的外部符号&#34;类 std :: basic_ostream&gt; &安培; __cdecl 运算符&lt;&lt;(class std :: basic_ostream
&amp;,类w1 :: Cstring const&amp;)&#34; (?? 6 @ YAAAV?$ basic_ostream @ DU?$ char_traits @ d @ STD @@@ STD @@ @ AAV01 @ ABVCstring W1 @@@ Z) 在函数中引用&#34; void __cdecl process(char *)&#34; (?process @@ YAXPAD @ Z)C:\ Users \ Chidi \ Documents \ Visual Studio 2013 \ Projects \ workshop1 \ workshop1 \ process.obj workshop1