我是C++/cli中的新手,所以我想清除一些事情。C++/cli無法使用運算符獲取屬性
在我的項目我使用下面的代碼:
//fileloader class
ref class FileLoader {
public:
property ProgressListeners^ DownloadProgressListeners {
ProgressListeners^ get() { return progress_listeners; }
}
static property FileLoader^ Instance {
FileLoader^ get() { return theSingleInstance; }
}
//some methods etc...
}
//progresslistener class
public interface class ProgressListener {
void DownloadProgressCallback(Object^ sender, DownloadProgressChangedEventArgs^ e);
};
public ref class ProgressListeners {
typedef List<ProgressListener^> ListenersList;
ListenersList^ listeners = gcnew ListenersList;
public:
void ProgressCallback(Object^ sender, DownloadProgressChangedEventArgs^ e) {
for each (ProgressListener^ listener in listeners)
listener->DownloadProgressCallback(sender, e);
}
void operator += (ProgressListener^ listener) { listeners->Add(listener);}
};
,並必須像這樣,casue,據我瞭解,獲得財產不能用作左值
// somewhere in code
ProgressListeners^ tmp = FileLoader::Instance->DownloadProgressListeners;
tmp += gcnew WindowsFormProgressListener(downloading_bar, m_log);
tmp += gcnew ConsoleProgressListener;
tmp += gcnew GUIProgressListener;
但它會更好,如果我能夠像這樣:
FileLoader::Instance->DownloadProgressListeners += gcnew WindowsFormProgressListener(downloading_bar, m_log);
FileLoader::Instance->DownloadProgressListeners += gcnew ConsoleProgressListener;
FileLoader::Instance->DownloadProgressListeners += gcnew GUIProgressListener;
我不知道如何牛逼Ø使此代碼正確的,希望我會找到這裏建議
當你試圖按照你想要的方式來做什麼時,你會得到什麼錯誤信息? (請不要讓我們猜。) –
@DavidYaw你在這裏 嚴重\t代碼\t說明\t項目\t文件\t線\t抑制狀態 錯誤\t C2676 \t二元「+」:「ProgressListeners ^」沒有定義這個操作符或轉換到類型接受的預定義運算\t啓動\t瓦特:\ unnamed_project \無名\啓動\ LauncherForm.h 但答案的問題解決了 – bobra