首页 技术 正文
技术 2022年11月6日
0 收藏 630 点赞 719 浏览 3470 个字

以下是我的代码:

//TaskConfigFile.h
#pragma once
using namespace System::Collections::Generic;
using namespace System;
using namespace System::IO;
using namespace System::Text;ref class TaskConfigFile
{
public:
TaskConfigFile();
TaskConfigFile(String^ str_link, Int64 file_size, short threads_sum);
TaskConfigFile(String^ str_link, Int64 file_size); String^ link;//下载链接
String^ fileName;//文件名
String^ filePath;//文件存储路径
Int64 fileSize;//文件总大小
Int64 blockSize;//文件分块大小
Int64 sumDownloadedSize;//已下载的总大小
short threadsSum;//线程总数,默认为5 //以字典记录各分块已下载的大小,key为分块的起始位置(字节),value为此分块已下载的大小
Dictionary<Int64, Int64>^ blockDownloadedSize;#ifdef DEBUG
Debug::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
Debug::AutoFlush = true;
#endif
bool Save();//保存配置信息
bool Load(String^ path);//加载配置信息
};
//TaskConfigFile.cpp
#include "stdafx.h"
#include "TaskConfigFile.h"TaskConfigFile::TaskConfigFile():
link(nullptr), fileName(nullptr), fileSize(0L), filePath(nullptr), blockSize(0L), sumDownloadedSize(0L), threadsSum()
{
blockDownloadedSize = gcnew Dictionary<Int64, Int64>(threadsSum);
}TaskConfigFile::TaskConfigFile(String^ str_link, Int64 file_size, short threads_sum):
link(str_link), fileSize(file_size), threadsSum(threads_sum), sumDownloadedSize()
{
blockDownloadedSize = gcnew Dictionary<Int64, Int64>(threadsSum);
}TaskConfigFile::TaskConfigFile(String^ str_link, Int64 file_size):
link(str_link), fileSize(file_size), threadsSum(), sumDownloadedSize()
{
blockDownloadedSize = gcnew Dictionary<Int64, Int64>(threadsSum);
}bool TaskConfigFile::Save()
{
String^ path = String::Concat(filePath, fileName, ".tmp");
Stream^ writeStream = gcnew FileStream(path, FileMode::Create, FileAccess::Write);
if(writeStream == nullptr)
{
#ifdef DEBUG
Diagnostics::Debug::WriteLine("文件路径错误!");
#endif
return false;
} BinaryWriter^ binaryWriter = gcnew BinaryWriter(writeStream, Encoding::ASCII);
binaryWriter->Write(this->link);
binaryWriter->Write(this->fileName);
binaryWriter->Write(this->filePath);
binaryWriter->Write(this->fileSize);
binaryWriter->Write(this->blockSize);
binaryWriter->Write(this->sumDownloadedSize);
binaryWriter->Write(this->threadsSum);
for each(KeyValuePair<Int64, Int64> pair in blockDownloadedSize)
{
binaryWriter->Write(pair.Key);
binaryWriter->Write(pair.Value);
}
writeStream->Close();
binaryWriter->Close();
return true;
}bool TaskConfigFile::Load(String^ path)
{
Stream^ readStream = gcnew FileStream(path, FileMode::Open, FileAccess::Read);
if(readStream == nullptr)
{
#ifdef DEBUG
Diagnostics::Debug::Indent();
Diagnostics::Debug::WriteLine("error: 打开文件失败!");
Diagnostics::Debug::WriteLine("Paht: {0}", path);
Diagnostics::Debug::UnIndent();
#endif
return false;
}
BinaryReader^ binaryReader = gcnew BinaryReader(readStream);
try
{
link = binaryReader->ReadString();
fileName = binaryReader->ReadString();
filePath = binaryReader->ReadString();
fileSize = binaryReader->ReadInt64();
blockSize = binaryReader->ReadInt64();
sumDownloadedSize = binaryReader->ReadInt64();
threadsSum = binaryReader->ReadInt16();
for(int i = ; i < threadsSum; ++i)
{
Int64 key = binaryReader->ReadInt64();
Int64 value = binaryReader->ReadInt64();
blockDownloadedSize->Add(key, value);
}
}
catch(EndOfStreamException^ ex)
{
#ifdef DEBUG
Diagnostics::Debug::Indent();
Diagnostics::Debug::WriteLine("The end of the stream is reached.");
Diagnostics::Debug::WriteLine(ex->Message);
Diagnostics::Debug::Unindent();
#endif
readStream->Close();
binaryReader->Close();
return true;
} return true;}

我将他们放在同一个文件就能编译通过。一旦分开就会出现链接错误:

C++/CLI中class成员声明与实现分开在不同文件时必须添加namespace

这是因为.NET以程序集作为编译单元,每一个程序集里类的成员声明与定义必须在同一个namespace下,而这两个文件中并没有声明namespace,所以链接器找不到TaskConfigFile Class成员的实现代码。

必须将它们声明在同一个namspace中:

//TaskConfigFile.h
namespace xxx
{
//..............
}//TaskConfigFile.cpp
using namespace xxx;
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,958
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,482
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,328
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,111
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,743
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,777