小编jua*_*nza的帖子

函数没有易失性。怎么修?

我在两个线程之间共享一个变量。我使用 volatile 来避免优化。

\n\n

但是,它显示了由于 strcpy 中没有 volatile 的错误。(如下)

\n\n

我怎样才能正确修复这个错误?

\n\n

有人告诉我要类型转换掉 易失性。\n但是如果我放弃易失性,那么我就失去了易失性的目的......\n最终可能会因优化而出现运行时错误......不是\'是吗?

\n\n

非常感谢。

\n\n

(代码可直接编译)

\n\n
\n\n
CRITICAL_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}\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

另一方面,我可以编写自己的strcpy来支持易失性。\n但这很奇怪。\n因为如果是这样,那么每次使用易失性时我都必须编写自己的I/O流(或那些复杂的函数)?

\n\n

再次感谢您的回答。

\n

c c++ volatile visual-c++

5
推荐指数
1
解决办法
526
查看次数

矢量的ofstream指针

这是我的问题,我需要创建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],我会想象我可以关闭它,其次,如果我不关闭它,程序工作正常,但我几乎可以肯定,这是一个不好的做法,不想成为一个懒惰或蹩脚的程序员,我尽可能多地看,但我没有找到答案.谢谢!!!

c++ error-handling pointers vector ofstream

5
推荐指数
2
解决办法
6211
查看次数

比较身份参考的标准方法

是否有一种标准方法来比较两个身份引用,基本上实现了以下操作:

bool compareForIdentity(int& a,int& b){return &a==&b;}
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
1
解决办法
8095
查看次数

枚举类:未命名值错误

我有

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 未命名值”

我的枚举中的某些名称出现此错误。

c++ visual-studio-2013

5
推荐指数
1
解决办法
832
查看次数

OpenCV 2.1:ostream operator << for cv :: Mat?

这个问题之后,我试图打印出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中的操作员是否可用,如果可以,我该如何获得?

c++ opencv

4
推荐指数
1
解决办法
2194
查看次数

使用#ifdefs指定平台

要在Mac和Windows之间处理特定于平台的代码,WIN32APPLE是要使用的术语,对吗?

所以,代码看起来像这样:

#ifdef _WIN32
    // Windows code
#endif
#ifdef __APPLE__
    // Mac code
#endif
Run Code Online (Sandbox Code Playgroud)

Linux怎么样?

我怎么能为这三个人做到这一点?对

c c++

4
推荐指数
1
解决办法
175
查看次数

普通的C++程序没有写入文件

这些行是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)

c++

4
推荐指数
1
解决办法
229
查看次数

const XX丢弃限定符[ - fpermissive]

在下面的代码片段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++ c++11

4
推荐指数
2
解决办法
7568
查看次数

何时通过引用传递并返回c ++中的函数

首先,我在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)

我不明白通过引用传递和返回有什么帮助?

c++

4
推荐指数
1
解决办法
79
查看次数

无法理解如何向std :: list &lt;std :: unique_ptr &lt;classname &gt;&gt;添加新对象

我对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添加到列表中。任何建议将非常欢迎。

c++ stl unique-ptr c++11

4
推荐指数
2
解决办法
1191
查看次数