我在两个线程之间共享一个变量。我使用 volatile 来避免优化。
\n\n但是,它显示了由于 strcpy 中没有 volatile 的错误。(如下)
\n\n我怎样才能正确修复这个错误?
\n\n有人告诉我要类型转换掉 易失性。\n但是如果我放弃易失性,那么我就失去了易失性的目的......\n最终可能会因优化而出现运行时错误......不是\'是吗?
\n\n非常感谢。
\n\n(代码可直接编译)
\n\nCRITICAL_SECTION CriticalSection;\n\nHANDLE hEvent;\n\nvoid __cdecl MyThread(void* name)\n\n{\n\nchar serName[256];\n\nvolatile char* vptr = (char*) name;\n\n\n\nEnterCriticalSection(&CriticalSection);\n\n\n\nstrcpy(serName, vptr); // error : cannot convert \'volatile\'\n\n// use (and not modify) name\xe2\x80\xa6\n\n\n\nLeaveCriticalSection(&CriticalSection);\n\nSetEvent (hEvent) ;\n\n}\n\n\n\n\nvoid main ()\n\n{\n\nchar name[256] = "abcde";\n\nhEvent = CreateEvent (NULL, false, false, NULL) ;\n\nif (!InitializeCriticalSectionAndSpinCount(&CriticalSection, 0x80000400) )\n\nreturn;\n\n\n\n_beginthread (MyThread, 0, name) ;\n\n\n\nEnterCriticalSection(&CriticalSection);\n\n// access name\xe2\x80\xa6\n\nLeaveCriticalSection(&CriticalSection);\n\n\n\nWaitForSingleObject (hEvent, INFINITE) ;\n\nDeleteCriticalSection(&CriticalSection);\n\nCloseHandle (hEvent);\n\nsystem("pause");\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n另一方面,我可以编写自己的strcpy来支持易失性。\n但这很奇怪。\n因为如果是这样,那么每次使用易失性时我都必须编写自己的I/O流(或那些复杂的函数)?
\n\n再次感谢您的回答。
\n这是我的问题,我需要创建X个文件并根据不同的事实写入它们,我的解决方案是创建一个像这样的ofstream指针向量
#include <boost/algorithm/string.hpp>
#include <vector>
#include<string>
#include <iostream>
#include <fstream>
vector<ofstream*> files;
files.resize(SplitVec.size()-4);
for(i=0;i<SplitVec.size()-4;i++)
{
line="/Users/jorge/Desktop/testing/strain_"+lexical_cast<string>(i);
cout<<"ine"<<endl;
files[i]=new ofstream(line.c_str());
}
Run Code Online (Sandbox Code Playgroud)
直到这一部分,它创建文件很好,后来在程序中我写给他们,这也很棒,我的问题是当我想要关闭对象,如果我使用:
for(i=0;i<SplitVec.size()-4;i++)
{
*files[i].close();
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
In file included from main.cpp:14:./methyl.h:298: error: request for member 'close' in 'files. std::vector<_Tp, _Alloc>::operator[] [with _Tp = std::ofstream*, _Alloc = std::allocator<std::ofstream*>](1ul)', which is of non-class type 'std::ofstream*'
Run Code Online (Sandbox Code Playgroud)
所以,我有一个问题,首先,为什么我不能调用close,它是指向ofstream的指针,所以使用*files [i],我会想象我可以关闭它,其次,如果我不关闭它,程序工作正常,但我几乎可以肯定,这是一个不好的做法,不想成为一个懒惰或蹩脚的程序员,我尽可能多地看,但我没有找到答案.谢谢!!!
是否有一种标准方法来比较两个身份引用,基本上实现了以下操作:
bool compareForIdentity(int& a,int& b){return &a==&b;}
Run Code Online (Sandbox Code Playgroud) 我有
enum class ErrorLevel
{
VERBOSE,
DEBUG_,
INFORMATION,
WARNING,
ERROR
};
Run Code Online (Sandbox Code Playgroud)
这有效:
assertDetectionParameters( parameterSet, ErrorLevel::WARNING );
Run Code Online (Sandbox Code Playgroud)
这不会:
assertDetectionParameters( parameterSet, ErrorLevel::ERROR );
Run Code Online (Sandbox Code Playgroud)
错误 1 错误 C2589:“常量”:“::”右侧的非法标记
错误 2 错误 C2059:语法错误:“::”
Resharper 说:
“ErrorLevel 未命名值”
我的枚举中的某些名称出现此错误。
在这个问题之后,我试图打印出cv::Mat标准输出的内容:
#include <iostream>
#include <opencv/cv.h>
#include <opencv/cv.hpp>
#include <opencv/cxcore.h>
#include <opencv/cxcore.hpp>
int main() {
cv::Mat m = cv::Mat::ones(10, 10, CV_32S);
std::cout << m << "\n";
}
Run Code Online (Sandbox Code Playgroud)
这导致错误
error: no match for ‘operator<<’ in ‘std::cout << m’
Run Code Online (Sandbox Code Playgroud)
我在Ubuntu 11.10上使用gcc 4.6.1,并按照这些说明安装了opencv,不包括示例.我的问题是,2.1中的操作员是否可用,如果可以,我该如何获得?
要在Mac和Windows之间处理特定于平台的代码,WIN32和APPLE是要使用的术语,对吗?
所以,代码看起来像这样:
#ifdef _WIN32
// Windows code
#endif
#ifdef __APPLE__
// Mac code
#endif
Run Code Online (Sandbox Code Playgroud)
Linux怎么样?
我怎么能为这三个人做到这一点?对
这些行是main()的唯一内容:
fstream file = fstream("test.txt", ios_base::in | ios_base::out);
string word;
while (file >> word) { cout << word + " "; }
file.seekp(ios::beg);
file << "testing";
file.close();
Run Code Online (Sandbox Code Playgroud)
程序正确地输出文件的内容("愤怒的狗"),但是当我之后打开文件时,它仍然只是说"愤怒的狗",而不是像我期望的那样"测试狗".
完整计划:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream file = fstream("test.txt", ios_base::in | ios_base::out);
string word;
while (file >> word) { cout << word + " "; }
file.seekp(ios::beg);
file << "testing";
file.close();
}
Run Code Online (Sandbox Code Playgroud) 在下面的代码片段1中,mKnownSRList定义如下:
std::vector<EndPointAddr*> mKnownSRList;
Run Code Online (Sandbox Code Playgroud)
我收到代码片段2中显示的编译错误.你能告诉我这段代码有什么问题吗?getTipcAddress()和compareTo函数的内容显示在下面的代码片段3和4中.
CODE SNIPPET 1(编译错误已标记)
void
ServiceRegistrarAPI::removeKnownSR(EndPointAddr & srEndPointAddr)
{
auto last =
std::remove_if(mKnownSRList.begin(),
mKnownSRList.end(),
[srEndPointAddr]( EndPointAddr* o )
{
//LINE 355 is the following
EndPointTipcAddr myTipcAddress = srEndPointAddr.getTipcAddress();
EndPointTipcAddr otherTipcAddress = o->getTipcAddress();
return (myTipcAddress.compareTo(otherTipcAddress));
});
if(*last != nullptr)
{
delete *last;
}
mKnownSRList.erase(last, mKnownSRList.end());
}
Run Code Online (Sandbox Code Playgroud)
SNIPPET 2(编译错误)
ServiceRegistrarAPI.cpp:355:72: error: passing ‘const EndPointAddr’ as ‘this’ argument of ‘EndPointTipcAddr& EndPointAddr::getTipcAddress()’ discards qualifiers [- fpermissive]
Run Code Online (Sandbox Code Playgroud)
CODE SNIPPET 3(getTipcAddress函数)
EndPointTipcAddr & getTipcAddress() { return mTipcAddress; }
Run Code Online (Sandbox Code Playgroud)
代码NIPPET 4(compareTo功能)
bool
EndPointTipcAddr::compareTo(EndPointTipcAddr &rhs) …Run Code Online (Sandbox Code Playgroud) 首先,我在c ++中创建了一个复杂的类,它有两个成员数据--real和imaginary.(形式为a + ib).当我试图为复杂类对象重载<<运算符如下 -
friend ostream operator<<(ostream ,complex ); in .h file
ostream operator <<(ostream o,complex c){
o<<"real part"<<c.real;
o<<"imaginary part<<c.imaginary;
return o;
}
Run Code Online (Sandbox Code Playgroud)
在.cpp文件中,它不起作用,而是打开一个ios_base文件并在那里显示错误.但是当我通过引用传递并通过引用返回如下时,相同的代码会完美地重载<<
ostream& operator <<(ostream& o,complex& c)
{
//same as before
};
Run Code Online (Sandbox Code Playgroud)
我不明白通过引用传递和返回有什么帮助?
我对unique_ptr的std :: list有一个奇怪的问题。
slFlyingMonster类是从slMonster类派生的。
以下代码有效:
std::unique_ptr<slMonster> ptr(new slFlyingMonster(md));
Run Code Online (Sandbox Code Playgroud)
但是这段代码:
std::list<std::unique_ptr<slMonster>> mMonsters;
mMonsters.push_back(new slFlyingMonster(md));
Run Code Online (Sandbox Code Playgroud)
引发错误:
“错误1错误C2664:'void
std :: list>,std :: allocator >>> :: push_back(const std :: unique_ptr <_Ty,std :: default_delete <_Ty >>&)':无法从'slFlyingMonster *'到'std :: unique_ptr> &&'“
据我了解,这是错误的,例如std :: list.push_back()与=不同,但是我无法弄清楚如何正确地将新类作为unique_ptr添加到列表中。任何建议将非常欢迎。