设计并编写一个名为
Text
的类,用于管理动态分配的字符串数组。在实例化时,Text
对象不接收任何内容或对不可修改的字符串的引用。该字符串包含文本文件的名称,该文件包含要存储在此类对象中的记录。如果该文件不存在,则Text
对象将采用安全的空状态。如果文件存在,则单参数构造函数为文件中包含的记录数分配内存并将其复制到内存中。要查看使用ifstream
对象从文本文件中读取的语法,请参阅名为自定义文件操作符的OOP244注释中的章节。另请参阅cplusplus.com。您的设计还包括以下成员函数:
- 复制构造函数
- 副本分配运算符
- 移动构造函数
- 移动分配操作员
- 一个析构函数
- 一个名为
size_t size() const
的成员函数,它返回文本数据的记录数在
namespace w3
中定义您的课程及其实施。
这是我的代码:
// Text.h
namespace w3{
class Text{
std::string file_name;
std::string* handler;
int no_of_rec=0;
public:
Text();
Text(char*);
size_t size() const;
Text(const Text& );
Text& operator=(const Text&);
Text(Text&&);
Text&& operator=(Text &&);
~Text();
}; // class Text
} //namespace w3
// Text.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include "Text.h"
namespace w3{
Text::Text(){
file_name="";
handler = nullptr;
no_of_rec = 0;
}
Text::Text(char* fname){
file_name = fname;
if (fname[0]='\0'){
file_name=" ";
std::cout << "can not find file name !!" <<std::endl;
}else{
std::fstream f(file_name);
std::string line;
if (f.is_open()){
while (std::getline(f,line,'\n')){
no_of_rec++;
}
}else{
std::cout << "file not open !!" <<std::endl;
}
//std::cout << "number of records" << size() << std::endl;
std::string* handle = new std::string[size()];
f.clear();
f.seekg(0,std::ios::beg);
int counter = 0;
while (std::getline(f,line,'\n')){
if (counter != no_of_rec){
handle[counter]=line;
counter++;
}
}
//std::cout << handle [1] <<std::endl;
}
}
size_t Text::size() const{
return (size_t)no_of_rec;
}
//-------------------Special Member Functions-----------
Text::Text(const Text& src){ //copy constructor
std::string file_name = src.file_name;
int no_of_rec = src.no_of_rec;
if (src.handler != nullptr){
handler = new std::string[src.size()];
handler = src.handler;
//std::cout << handler[123]<<std::endl;
}else{
handler = nullptr;
}
}
Text& Text::operator=(const Text& src){
if (this != &src){
int no_of_rec = src.no_of_rec;
std::string file_name = src.file_name;
if (src.handler != nullptr){
handler = new std::string[src.size()];
handler=src.handler;
}else{
handler = nullptr;
}
}
return *this;
}
Text::Text(Text&& src){
file_name=src.file_name;
handler = src.handler;
no_of_rec = src.no_of_rec;
src.file_name=" ";
src.handler=nullptr;
src.no_of_rec=0;
}
Text&& Text::operator=(Text&& src){
if (&src != this){
file_name=src.file_name;
handler = src.handler;
no_of_rec = src.no_of_rec;
src.file_name=" ";
src.handler=nullptr;
src.no_of_rec=0;
}
return std::move(*this);
}
Text::~Text(){
//delete [] handler;
}
}
// main.cpp中
#include <iostream>
#include <iomanip>
#include <utility>
#include <ctime>
// #include "Text.h"
#include "Text.cpp"
#define TIME(start, end) double((end) - (start)) / CLOCKS_PER_SEC
int main (int argc, char* argv[]) {
if (argc == 1) {
std::cerr << argv[0] << ": missing file operand\n";
return 1;
}
else if (argc != 2) {
std::cerr << argv[0] << ": too many arguments\n";
return 2;
}
std::clock_t cs, ce;
{
std::cout << std::fixed << std::setprecision(3);
cs = std::clock();
w3::Text a;
ce = std::clock();
std::cout << "Constructor " << TIME(cs, ce) << " seconds";
std::cout << " - a.size = " << a.size() << std::endl;
cs = std::clock();
w3::Text b(argv[1]);
ce = std::clock();
std::cout << "Constructor " << TIME(cs, ce) << " seconds";
std::cout << " - b.size = " << b.size() << std::endl;
cs = std::clock();
a = b;
ce = std::clock();
std::cout << "Copy Assignment " << TIME(cs, ce) << " seconds";
std::cout << " - a.size = " << a.size() << std::endl;
cs = std::clock();
a = std::move(b);
ce = std::clock();
std::cout << "Move Assignment " << TIME(cs, ce) << " seconds";
std::cout << " - a.size = " << a.size() << std::endl;
cs = std::clock();
w3::Text c = a;
ce = std::clock();
std::cout << "Copy Constructor " << TIME(cs, ce) << " seconds";
std::cout << " - c.size = " << c.size() << std::endl;
cs = std::clock();
w3::Text d = std::move(a);
ce = std::clock();
std::cout << "Move Constructor " << TIME(cs, ce) << " seconds";
std::cout << " - d.size = " << d.size() << std::endl;
cs = std::clock();
}
ce = std::clock();
std::cout << "Destructor " << TIME(cs, ce) << " seconds\n";
}
我遇到错误,Text.cpp中有多个Text定义
我知道我的命名空间有问题,但我不知道如何修复它。
答案 0 :(得分:0)
您必须以“w3 :: Text”格式访问.cpp文件中的Text类,而不是“Text”。
例如:w3 :: Text :: Text(const Text&amp; src){//复制构造函数 取而代之的是:Text :: Text(const Text&amp; src){// copy constructor