标签: most-vexing-parse

使用istream_iterator范围构造时无法访问向量


我试图编译这段代码片段但是我遇到了编译错误:(!用Visual Studio 2010编译

#include <vector>
#include <string>
#include <sstream>
#include <iterator>
#include <iostream>

using namespace std;

int main() {
    string s( "Well well on" );
    istringstream in( s );
    vector<string> v( istream_iterator<string>( in ), istream_iterator<string>() );
    copy( v.begin(), v.end(), ostream_iterator<string>( cout, "\n" ) );
}
Run Code Online (Sandbox Code Playgroud)

错误:

Error   1   error C2228: left of '.begin' must have class/struct/union  c:\visual studio 2008 projects\vector test\vector test\main.cpp 13  vector test
Error   2   error C2228: left of '.end' must have class/struct/union    c:\visual studio 2008 projects\vector test\vector …
Run Code Online (Sandbox Code Playgroud)

c++ stl most-vexing-parse

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

默认构造函数c ++

我试图理解默认构造函数(由编译器提供,如果你不写一个)与你自己的默认构造函数的工作原理.

所以我举了这个简单的类:

class A
{
    private:
        int x;
    public:
        A() { std::cout << "Default constructor called for A\n"; }
        A(int x)
        {
            std::cout << "Argument constructor called for A\n";
            this->x = x;
        }
};

int main (int argc, char const *argv[])
{
    A m;
    A p(0);
    A n();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

为A调用的默认构造函数

参数构造函数调用A

所以对于最后一个,有一个被调用的另一个构造函数,我的问题是在这种情况下哪个类型和哪个类型具有?

c++ constructor default-constructor most-vexing-parse

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

构造函数被解释为函数调用,并且直接跳过了构造函数

我有一个包含4个构造函数的类,函数如下:

using namespace std;

class ABC {
public:
    ABC() {
        cout << "ABC()\n";
    }

    ABC(int) {
        cout << "ABC(int)\n";
    }

    ABC(ABC&) {
        cout << "ABC(&)\n";
    }

    ABC(ABC&&) {
        cout << "ABC(&&)\n";
    }
};


void ff(ABC t) { }
Run Code Online (Sandbox Code Playgroud)

请帮我解释一些对我来说很奇怪的行为(我使用MSVC 2016进行编译):

1)为什么我收到警告C4930:"'ABC a1(ABC(__ cdecl*)(void))':未调用prototyped函数(是一个变量定义?)",使用以下代码:

void main() {
    ABC a1(ABC());
    ff(ABC(5));
}
Run Code Online (Sandbox Code Playgroud)

在执行时,我希望得到以下输出:

ABC()
ABC(&&)
ABC(int)
ABC(&&)
Run Code Online (Sandbox Code Playgroud)

但我真正得到的是

ABC(int)
Run Code Online (Sandbox Code Playgroud)

2)现在,如果我改为

void main() {
    ABC a1(ABC(5));
    ff(ABC(5));
}
Run Code Online (Sandbox Code Playgroud)

没有更多的警告.但在执行时,我期望得到的是

ABC(int)
ABC(&&)
ABC(int)
ABC(&&)
Run Code Online (Sandbox Code Playgroud)

但我真正得到的是

ABC(int)
ABC(int)
Run Code Online (Sandbox Code Playgroud)

3)现在

void main() {
    ABC( ABC() ); …
Run Code Online (Sandbox Code Playgroud)

c++ constructor most-vexing-parse move-constructor

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

在C ++中设置构造函数默认值

可能重复:
为什么使用一组空括号来调用不带参数的构造函数会出错?
构造对象和调用成员函数

最近,我对构造函数有了一些了解,但是今天,在设置构造函数参数的默认值时遇到了麻烦。

hpp文件中的构造函数声明如下所示:

class PlayingCard {
public:
    PlayingCard(int value = 0, int suit = 0); //default values supplied
Run Code Online (Sandbox Code Playgroud)

cpp文件中的定义如下所示:

PlayingCard::PlayingCard(int value, int suit) :  _faceValue(value), _suit(suit)
{}
Run Code Online (Sandbox Code Playgroud)

_faceValue并且_suit是PlayingCard的私人成员)当我像这样测试时:

PlayingCard card1();
PlayingCard card2(7, 1);

cout << "suit int value: " << card1.getSuit() <<
        " face value: " << card1.getFaceValue() << endl;
cout << "suit int value: " << card2.getSuit() <<
        " face value: " << card2.getFaceValue() << endl;
Run Code Online (Sandbox Code Playgroud)

如果我注释掉有关card1的代码,则card2可以正常工作。否则我得到错误: 请求'card1'中的成员'getSuit',它是非类类型'PlayingCard()'。

哪个必须意味着它不能识别没有参数的构造函数的使用?我究竟做错了什么?

c++ constructor default most-vexing-parse

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

奇怪的未解析的外部编译DLL

我试图按照本网站上的说明操作:http: //support.microsoft.com/kb/q168958

这是代码:

#include <windows.h>
#include <malloc.h>

namespace Test {

template<class TYPE> class TestBuffer
{
  private:
    TYPE*   m_pData;
    size_t  m_uSize;

  public:
    TestBuffer(size_t uSize) : m_pData(NULL), m_uSize(0)
    {
      m_pData = (TYPE*)malloc(uSize * sizeof(TYPE));
    }

    ~TestBuffer() { if (NULL != m_pData) free(m_pData); }

  public:
    bool IsValid() const { return NULL != m_pData; }
    operator TYPE*() const { return m_pData; }
};

template class __declspec(dllexport) Test::TestBuffer<wchar_t>;

__declspec(dllexport) bool GetCurrentDir(char* szDir, size_t uSize)
{
  DWORD dwRequiredSize = ::GetCurrentDirectoryW(0, NULL);
  Test::TestBuffer<wchar_t> wideCharBuffer(size_t(dwRequiredSize));

  bool …
Run Code Online (Sandbox Code Playgroud)

c++ windows dll unresolved-external most-vexing-parse

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

这个语法是什么意思

以下代码编译.但是,如果我编写代码来test使用jar它调用方法,则会给我一个编译错误.这里真的发生了什么.

#include <iostream>    

using namespace std;

class A {
public:
  void test() {
    cout << "working" << endl;
  }
};

int main() {
  A foo;
  A jar();
}
Run Code Online (Sandbox Code Playgroud)

c++ most-vexing-parse

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

错误:请求 '..' 中的成员 '..' ,这是非类类型

我使用 STLpriority_queue并提供一个自定义比较器类,其构造函数接收指向存储优先级的向量的指针,因此 -

#include <iostream>
#include <queue>          // std::priority_queue
#include <vector>         // std::vector

using namespace std;

class CompareReachDist
{
    const vector<float> *reach_dists;
public:
    CompareReachDist(const vector<float> *input)
    {
        reach_dists = input;
    }

    bool operator() (const size_t &l, const size_t &r) const
    {
        return (reach_dists->at(l) > reach_dists->at(r));
    }
};

typedef priority_queue<size_t, vector<size_t>, CompareReachDist> pq;
vector<float> reach_dists;

int main()
{
    pq seeds(CompareReachDist(&reach_dists));
    bool isEmpty = seeds.empty();

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,在编译时出现错误:

error: request for member 'empty' in 'seeds', which is of non-class …
Run Code Online (Sandbox Code Playgroud)

c++ syntax initialization variable-initialization most-vexing-parse

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

不清楚最令人讨厌的解析

让我们假设以下类Foo。

struct Foo
{
  int i;
};
Run Code Online (Sandbox Code Playgroud)

如果要创建此类的实例并对其进行初始化,则应该执行以下操作:Foo foo1 = Foo();调用构造函数。

int main(void)
{
    foo1 = Foo(); 
    cout << foo1.i << " : " << foo1.j << endl; // " 0 " 
}
Run Code Online (Sandbox Code Playgroud)

然后我以为我会用以下代码行调用默认构造函数,但事实并非如此:

int main(void)
{
    Foo foo2(); // most vexing parse
    cout << foo2  << endl; // " 1 " ??? why?
    foo2.i++; // error: request for member ‘i’ in ‘foo2’, which is of non-class type ‘Foo()’ 
}

Run Code Online (Sandbox Code Playgroud)

为什么foo2初始化为int(1)而不是Foo()?

我知道最烦人的解析,它告诉我,当我将一行解释为一个函数时,它就被解释为一个函数。

但是foo1()被解释为一个int,而不是一个函数,也不是一个Foo类。

之所以编译Foo foo2()行是因为它将其解释为函数原型。好。

为什么要编译此行? cout << …

c++ constructor struct default-constructor most-vexing-parse

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

使用 C++ 中的 RAII 在线程包装类中移动语义

我试图让一个围绕 std::thread 的 RAII 包装类在 C++ 中工作。

#include <iostream>
#include <thread>
#include <vector>

using std::cout;
using std::endl;

void threadFunction()
{
    for (int i = 0; i < 20; ++i) {
        cout << "thread function pointer executing..." << i << endl;
    }
}



// RAII wrapper that supports automatic join
class ThreadJoinRAII
{
    std::thread thread_;
public:
    ThreadJoinRAII(std::thread&& t):thread_(std::move(t)) 
    // ThreadJoinRAII()
    {
        cout << "ThreadJoinRAII ctor" << endl;
    }

    //ThreadJoinRAII(const ThreadJoinRAII& other) = delete;
    //ThreadJoinRAII& operator=(const ThreadJoinRAII& other) = delete;

    //ThreadJoinRAII(ThreadJoinRAII&& other):thread_(std::move(other.thread_)) …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading move most-vexing-parse

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

查找模板参数的typeid

构造函数定义中的print语句没有打印,是不是main中构造函数调用正确?我知道我在这里错过了一些观点,请指出.

#include <iostream>
#include <typeinfo>

template <typename T> class List
{
    public: 
        template <typename T2> List (List<T2> const&);
}; 

template <typename T> template <typename T2> List <T> :: List (List <T2> const&) 
{
    std :: cout << "\nType name:" << typeid (T2).name();
}

int main ()
{
    List <int> kk (List <int>);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates typeid most-vexing-parse

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

重载operator <<输出bool值.为什么?

xml_attribute.h

#pragma once
#ifndef XML_ATTRIBUTET_H
#define XML_ATTRIBUTET_H

#include <string>
#include <iostream>

struct XML_AttributeT{

    std::string tag;
    std::string value;

    //constructors
    explicit XML_AttributeT(std::string const& tag, std::string const& value);
    explicit XML_AttributeT(void);

    //overloaded extraction operator
    friend std::ostream& operator << (std::ostream &out, XML_AttributeT const& attribute);
};
#endif
Run Code Online (Sandbox Code Playgroud)

xml_attribute.cpp

#include "xml_attribute.h"

//Constructors
XML_AttributeT::XML_AttributeT(std::string const& tag_, std::string const& value_)
: tag{tag_}
, value{value_}
{}
XML_AttributeT::XML_AttributeT(void){}

//overloaded extraction operator
std::ostream& operator << (std::ostream &out, XML_AttributeT const attribute){
    return out << attribute.tag << "=" << attribute.value;
}
Run Code Online (Sandbox Code Playgroud)

driver.cpp

#include <iostream> …
Run Code Online (Sandbox Code Playgroud)

c++ reference operator-overloading most-vexing-parse c++11

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

非const引用的初始化无效

好吧,我正试图弄清楚这个错误,到目前为止,绝对没有运气.我这是为了做作业,这就是为什么我没有使用包含的课程.

这是相关的代码:

//Will return an array where each element is the sum of the respective sums in the added         arrays
Vec& Vec::operator+(Vec& v2) const{
    Vec ret();
    if(mySize>0){
        Vec ret(mySize);
        for(unsigned i = 0;i<mySize;i++){
            ret[i]=v2[i]+myArray[i];
        }
    }
    else{
    }
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

从.h文件......:

Vec& operator+ (Vec& v2) const;
Run Code Online (Sandbox Code Playgroud)

这会引发错误:"从'Vec(*)()'类型的右值''类型'Vec&'类型的非const引用无效初始化"我对C++完全不熟悉,所以任何帮助都会受到赞赏.

c++ initialization most-vexing-parse

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