小编Ele*_*rks的帖子

为什么编译器声称这个类方法没有返回值?

我从Eclipse得到的错误是"没有返回,函数返回非void".我添加了默认:case以查看它是否会带走错误,但没有bean.我认为返回rvalue是可以的,因为它会被重复复制到堆栈中,因此当getLogLevelName()的局部变量超出范围时,将存在字符串的副本.确实,调用代码有效,但错误对我来说很神秘.

std::string bmd2::Logger::getLogLevelName(bmd2::Logger::LogLevel logLevel) throw ()
{
  switch (logLevel)
  {
    case bmd2::Logger::LogLevel::LOG_ERROR:
      return std::string ("ERROR");
      break;
    case bmd2::Logger::LogLevel::LOG_WARNING:
      return std::string ("WARNING");
      break;
    case bmd2::Logger::LogLevel::LOG_INFO:
      return std::string ("INFO");
      break;
    case bmd2::Logger::LogLevel::LOG_DEBUG:
    default:
      return std::string ("DEBUG");
      break;
  }
}
Run Code Online (Sandbox Code Playgroud)

c++ enums switch-statement c++11

2
推荐指数
1
解决办法
212
查看次数

在c ++ 11中将std :: string转换为char*

我正在为混合语言编写一个库,因此我们必须坚持使用C接口.我需要调用这个API函数:

void getproperty(const char * key, /* in  */ 
                 char * value      /* out */) {
Run Code Online (Sandbox Code Playgroud)

我需要使用std :: string的内容设置.

std::string stdString = funcReturningString();
// set value to contents of stdString
Run Code Online (Sandbox Code Playgroud)

我已经看到很多关于可能策略的答案,但没有针对这个特定的用例,也没有被认为是"最佳"或"标准".我们正在使用C++ 11,它的价值.有些答案含糊地建议将字符串的内容复制到缓冲区并使用它.但怎么办呢?我想对这次谈话有更多的意见,所以我最终可以选择一个策略并坚持下去.

谢谢!

更新:这是我使用的解决方案

我使用uint32_t与其他API函数保持一致.bufferSize是一个引用,因为FORTRAN通过引用调用所有内容.

void getproperty(const char * key,            /* in  */ 
                 const uint32_t & bufferSize, /* in  */
                 char * value,                /* out */
                 uint32_t & error             /* out */) 
{
  std::string stdString = funcReturningString();

  if (bufferSize < str.size() + 1) {
    // Set the error …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

2
推荐指数
1
解决办法
4724
查看次数

从模板类外部获取typename T.

我有一个矢量,我想包装一些额外的功能:

template <typename T>
class PersistentVector : PersistentObject
{
  private:
    std::vector<T> values;

  public:
    virtual void read();
Run Code Online (Sandbox Code Playgroud)

现在,如果必须知道类型名称T,我将如何在类外部定义read()?

第一次尝试:

void PersistentVector::read()
{
  // How can I get the iterator type?
  typedef std::vector<T>::iterator it_type;  // vector cannot be resolved
}
Run Code Online (Sandbox Code Playgroud)

第二次尝试:

// error: Member declaration not found
template <typename T>
void PersistentVector::read()
{
  typedef std::vector<T>::iterator it_type;  // no error
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

2
推荐指数
1
解决办法
203
查看次数

std :: map :: at在调用堆栈中的某处返回警告"返回临时引用"

我正在查找一个键的值std::map.我在Dataset :: getProperty()中得到以下错误:

returning reference to temporary [-Wreturn-local-addr]

调用堆栈中的哪些是临时创建的?我以为std::map::at返回了对左值的引用,因为我不断返回reference-to-const原来的调用者会引用左值.

数据集

class Dataset
{
  private:
    PropertyCollection properties;

  public:
    const std::string & getProperty(const std::string & key) const;

  ...
}

const std::string & Dataset::getProperty(const std::string & key) const
{
  // WARNING: returning reference to temporary [-Wreturn-local-addr]
  return properties.getProperty(key);
}
Run Code Online (Sandbox Code Playgroud)

PropertyCollection

class PropertyCollection
{
  private:
    std::map<std::string, std::string> properties;

  public:
    const bmd2::string & getProperty(const bmd2::string & key) const

  ...
}

const std::string & PropertyCollection::getProperty(const std::string & key) 
      const
{ …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

2
推荐指数
1
解决办法
513
查看次数

为什么以及何时更改值?

请注意以下代码.我从std::stringc-style字符串转而回到std::string原因bmd2create是因为它是C Binding API的一部分,它必须采用c风格的字符串.否则我std::string尽可能使用.

OtherFile.h

void bmd2create(const char * filename) {
  std::string sFileName (filename);

  // do things with sFileName

  std::ostringstream ostr;
  ostr << "\t(in)  filename       = " << filename << "\n";
  logger.log(Logger::LogLevel::LOG_DEBUG, ostr.str());
}
Run Code Online (Sandbox Code Playgroud)

Main.cpp的

const char * filename = std::string(filepath + "dataset1.out").c_str();

// *filename is 46 '.'

bmd2create(filename);

// *filename is now 0 '\0'
Run Code Online (Sandbox Code Playgroud)

这个问题

文件名指针移动的位置和原因在哪里?什么是将其移回字符串开头的最佳方法?

c++

2
推荐指数
1
解决办法
75
查看次数

Jersey/JAX-RS中的嵌套内部类

我有这个完美的资源:

@Path("/adoptable")
public class AdoptableAnimalsResource {

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String get() 
  {
    return "dogs";
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,我怎样才能将这个类变成嵌套的内部类? 例如,

public class Grouper
{
  @Path("/adoptable")
  public class AdoptableAnimalsResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String get() 
    {
      return "dogs";
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试它时,我得到404 Not Found错误,表明Jersey不会将内部类视为资源.

java rest web-services jax-rs jersey

2
推荐指数
1
解决办法
1294
查看次数

为什么这个头文件编译时缺少#include?

人.h

#ifndef PERSON_H_
#define PERSON_H_

/* Person.h */

class Person {
  int age;
  std::string name;

  public:
    Person(int, std::string);
    std::string getName();
    int getAge();

};

#endif /* PERSON_H_ */
Run Code Online (Sandbox Code Playgroud)

person(int std::string) 函数声明使用 std::string 名称,但我尚未将其包含在头文件中。因此,我希望编译器会抱怨丢失的符号。但它编译并运行良好!为什么?

其余代码...

个人.cpp

#include <string>
#include "Person.h"

Person::Person(int age, std::string name)
{
  this->name = name;
  this->age = age;
}

std::string Person::getName()
{
  return this->name;
}

int Person::getAge()
{
  return this->age;
}
Run Code Online (Sandbox Code Playgroud)

主程序

#include <string>
#include "Person.h"

int main() {

  printFunc();

  Person chelsea_manning(5, std::string("Chelsea Manning"));

}
Run Code Online (Sandbox Code Playgroud)

另外,我是 C++ 的新手,所以如果你发现我的代码/OOP 有什么奇怪的地方,请告诉我。

c++

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

Windows API - 使用WriteFile编写std :: string的正确方法是什么?

BOOL WINAPI WriteFile(
  _In_         HANDLE hFile,
  _In_         LPCVOID lpBuffer,
  _In_         DWORD nNumberOfBytesToWrite,
  _Out_opt_    LPDWORD lpNumberOfBytesWritten,
  _Inout_opt_  LPOVERLAPPED lpOverlapped
);
Run Code Online (Sandbox Code Playgroud)

WriteFile采用const void*.如何将std :: string转换为const void*,以便将字符串的内容写入磁盘?

c++ winapi

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

如何使用Exception C++第1章第1部分中的copy()算法?

Herb提出了一种循环向量的方法:

for(vector<int>::iterator i = v.begin(); i < v.end(); i++) {
 cout << *i << endl;
}
Run Code Online (Sandbox Code Playgroud)

他将此代码替换为:

copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\n"));
Run Code Online (Sandbox Code Playgroud)

我很难理解这是如何工作的,或者为什么会这样.我查找了复制功能,文档说它相当于:

template<class InputIterator, class OutputIterator>
  OutputIterator copy (InputIterator first, InputIterator last, 
                       OutputIterator result)
{
  while (first!=last) {
    *result = *first;
    ++result; ++first;
  }
  return result;
}
Run Code Online (Sandbox Code Playgroud)

所以我提出了一个问题,"当我们*OutputIterator会发生什么?"

reference operator*() const;

Dereference iterator
Returns *this.
Run Code Online (Sandbox Code Playgroud)

这就是我感到困惑的地方.我没有看到OutputIterator指向的定义.另外,我没有看到该行如何 *result = *first;可能转换为调用cout << *i;

c++ stl copy vector

0
推荐指数
1
解决办法
73
查看次数

有没有办法判断是否已分配内存缓冲区并且必须删除?

我想出了一个聪明(或愚蠢)的设计模式.在我们的一个用例中,可能需要创建一个大型内存缓冲区,该缓冲区将在类实例的生命周期中反复使用.在我们不需要内存缓冲区的情况下,我想避免创建它.所以我想出了以下内容:

class Class {
  public:
    void func()
    {
      if (A) {
        // do something
      } else if (B) {
        // need data buffer, which we will recycle
        static float * data = new float[1000000];
        // do something with the data
      } else {
        ...
      }
    }

  ~Class() {
    // to delete[] or not to delete[] that is the question
  }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在析构函数中释放此缓冲区?我可以使用delete[],但问题是:我们怎么知道是否必须删除缓冲区?换句话说,有没有一种聪明的方法可以知道是否if (B)被执行了?当然我可以用旗帜做到这一点,但我不确定是否有更好的方法来做到这一点.堆上的静态内存缓冲区的概念可能会或可能不会被初始化,这对我来说有点令人困惑.

c++

0
推荐指数
1
解决办法
68
查看次数

为什么这些成员没有默认初始化?

在以下代码中,我收到以下警告消息:

成员'年'未在此构造函数中初始化

成员'month'未在此构造函数中初始化

成员'日'未在此构造函数中初始化

...

但是,我希望成员默认初始化为uint32_t类型,例如0.

typedef uint32_t bmd2_uint32;

class UnitTestCInterface : public UnitTest {
{
  private:
    bmd2_uint32 year;
    bmd2_uint32 month;
    bmd2_uint32 day;
    ...

  public:
    UnitTestCInterface() 
      : filename(nullptr), UnitTest("UNIT TEST: C Interface") { };
Run Code Online (Sandbox Code Playgroud)

为什么这些成员没有被默认初始化,我该怎么办呢?

c++

0
推荐指数
1
解决办法
111
查看次数

什么是C++中的独立冒号

规范的Qt示例是:

class Counter : public QObject
{
    Q_OBJECT
    int m_value;
public:
    int value() const { return m_value; }
public slots:
    void setValue(int value);
signals:
    void valueChanged(int newValue);
};
Run Code Online (Sandbox Code Playgroud)

Qt定义宏:

#define slots /* nothing */
Run Code Online (Sandbox Code Playgroud)

预处理器运行后,我们将留下:

public slots:
  void setValue(int value);
:
  void valueChanged(int newValue);
};
Run Code Online (Sandbox Code Playgroud)

独立式结肠有什么影响?我可以在任何我想要的地方添加冒号吗?

int main() {
:
std::cout << "hi";
:
return 0;
}
Run Code Online (Sandbox Code Playgroud)

独立冒号在C++中做什么,为什么允许?

c++

0
推荐指数
1
解决办法
92
查看次数

找不到隐式类方法

我有一个带隐式类的对象:

object ModelUtils {
  implicit class RichString(str: String) {
    def isNullOrEmpty(x: String): Boolean = x == null || x.trim.isEmpty
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用它时,IntelliJ找不到isNullOrEmpty方法:

"TestString".isNullOrEmpty
Run Code Online (Sandbox Code Playgroud)

我尝试了各种导入导入方法无济于事.我错过了什么?

scala implicit

0
推荐指数
1
解决办法
57
查看次数

标签 统计

c++ ×11

c++11 ×4

copy ×1

enums ×1

implicit ×1

java ×1

jax-rs ×1

jersey ×1

rest ×1

scala ×1

stl ×1

switch-statement ×1

vector ×1

web-services ×1

winapi ×1