小编Tri*_*ant的帖子

可以从同一类型的另一个对象访问私有变量吗?

可能重复:
使用私有修饰符,为什么可以直接访问其他对象中的成员?

C++类的私有成员被设计为对其他类实例不可见.我很困惑,因为可以访问私人成员,如下所示!有谁可以向我解释一下?

这是我的代码:

#include <iostream> 
using namespace std; 
class Person
{
private:
    char* name;
    int age;
public:
    Person(char* nameTemp, int ageTemp)
    {
      name = new char[strlen(nameTemp) + 1];
      strcpy(name, nameTemp);
      age = ageTemp;
    }
    ~Person()
    {
      if(name != NULL)
        delete[] name;
      name = NULL;
    }
    bool Compare(Person& p)
    {
      //p can access the private param: p
      //this is where confused me
      if(this->age < p.age) return false;
        return true;
    }
};
int main() 
{ 
  Person p("Hello, world!", 23); …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++ c++11

12
推荐指数
2
解决办法
2万
查看次数

使用decltype(var)后跟内部类型"var"时出现C++ 11编译器错误

我正在使用Visual C++ 2010,这是我的代码片段:

std::set<int> s;
decltype(s)::value_type param = 0;
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息,任何人都可以帮助我?

> error C2039: 'value_type' : is not a member of '`global namespace''
> error C2146: syntax error : missing ';' before identifier 'param'
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors visual-studio-2010 decltype c++11

8
推荐指数
2
解决办法
1273
查看次数

我怎样才能避免编译错误:std :: transform?

这是我的C++代码(我使用的是Visual C++ 2010):

int absd(int t)
{
    return abs(t);
}
int main()
{
    try
    {
        int dpi = 137;
        int dpiCriterionAry[] = {100, 150, 200, 300, 400, 500, 600};
        std::vector<int> vec(dpiCriterionAry, dpiCriterionAry + _countof(dpiCriterionAry));
        std::transform(vec.begin(), vec.end(), vec.begin(), std::bind1st(std::minus<int>(), dpi));
        std::transform(vec.begin(), vec.end(), vec.begin(), absd);
        //std::transform(vec.begin(), vec.end(), vec.begin(), abs);
        copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, "\t"));
        cout << endl;
    }
    catch(exception& e)
    {
        cerr << e.what() << endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我取消注释该行时:

//std::transform(vec.begin(), vec.end(), vec.begin(), abs);
Run Code Online (Sandbox Code Playgroud)

我收到了错误消息:

1>------ Build started: Project: Console, Configuration: Release …
Run Code Online (Sandbox Code Playgroud)

c++ stl compiler-errors visual-c++ genetic-algorithm

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

如何使用__stdcall来限定C++ lambda?

前言 - 我喜欢C++ lambda,如果可能的话,我将在任何地方使用它.

现在我有一个lambda要求,我需要一个__stdcall lambda.但是我收到以下错误消息:

 error C2664: 'EnumWindows' : cannot convert parameter 1 from '`anonymous-namespace'::<lambda1>' to 'WNDENUMPROC'
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Run Code Online (Sandbox Code Playgroud)

有人可以帮帮我吗?这是我的代码(EnumWindowsProc在函数范围内):

        auto EnumWindowsProc = 
            [&](HWND hwnd, LPARAM lParam) mutable -> bool
        {
            return true;
        };
        EnumWindows(EnumWindowsProc, NULL);
Run Code Online (Sandbox Code Playgroud)

c++ windows lambda visual-studio-2010 c++11

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

std::integral_constant&lt;ch​​ar*, "kernel32.dll"&gt; 无法编译?

我想从 char* 和“kernel32.dll”中得到积分常数,但总是失败。以下是我失败的尝试,谁能告诉我正确的用法?

error 1: cout << std::integral_constant<const char*, "kernel32.dll">::value << endl;
error 2: cout << std::integral_constant<char*, "kernel32.dll">::value << endl;
error 3: cout << std::integral_constant<char[], "kernel32.dll">::value << endl;
error 4: cout << cout << std::integral_constant<char*, static_cast<char*>("kernel32.dll")>::value << endl;
Run Code Online (Sandbox Code Playgroud)

以上 4 条语句具有相同的错误信息。:

Console.cpp(181): error C2762: 'std::integral_constant' : invalid expression as a template argument for '_Val'
1>          D:\Programfiles\Visual Studio 2013\VC\include\xtr1common(35) : see declaration of 'std::integral_constant'
1>Console.cpp(181): error C2955: 'std::integral_constant' : use of class template requires template argument list
1>          D:\Programfiles\Visual Studio 2013\VC\include\xtr1common(35) …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11 visual-studio-2012

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

是否可以设置std :: tr1 :: tuple的默认值?

(我正在使用Visual C++ + 2010)假设我已经定义了这样的元组:

typedef std::tr1::tuple<
int      //i want to set its default value to 9
, double //i want to set its default value to 3.3
, int    //i want to set its default value to 2
, double //i want to set its default value to -7.2
> Mytuple; 
Run Code Online (Sandbox Code Playgroud)

我可以在结构中做到这一点.但我想知道是否可以这样做std::tr1::tuple.
此外,我想知道什么时候使用std::tr1:tuple或者struct?

有人可以帮帮我吗?

c++ std visual-studio-2010 visual-c++ c++11

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

__int64格式化输出为十六进制

我想将__int64类型的param转换为CString.但我总是得到错误的输出.这是我的代码:

__int64 = offset;//non zero
CString strOutput;
strOutput.Format(_T("0x%x"), offset);
Run Code Online (Sandbox Code Playgroud)

错误的输出是:0x0

有人可以帮帮我吗?

c++ mfc visual-studio-2010 visual-c++

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