小编son*_*yao的帖子

在C++中多次声明函数和变量

在C++中,多次声明变量会在编译期间显示错误.例如:

int x;
int x;
Run Code Online (Sandbox Code Playgroud)

在多次声明函数时,编译期间不会显示任何错误.例如:

int add(int, int);
int add(int, int);
Run Code Online (Sandbox Code Playgroud)

为什么在C++中有这种区别?

c++ declaration definition one-definition-rule

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

C++中的朋友和模板

我的C++代码示例中存在一个大问题."朋友"和"模板"有问题.

错误消息:
Matrix.h:26:79:警告:

friend声明'std :: ostream&matrixClass :: operator <<(std :: ostream&,const matrixClass :: Matrix&)'声明一个非模板函数[-Wnon-template-friend] friend std :: ostream&operator <<(std :: ostream&,const Matrix&matrix);

Matrix.h:26:79:注意:

  (if this is not what you intended, make sure the function template
Run Code Online (Sandbox Code Playgroud)

已声明并在此处添加<>后的函数名称)

Matrix.h:28:77:警告:

  friend declaration 'matrixClass::Matrix<T>*
Run Code Online (Sandbox Code Playgroud)

matrixClass :: operator*(const matrixClass :: Matrix&,const matrixClass :: Matrix&)'声明一个非模板函数[-Wnon-template-friend]朋友Matrix*operator*(const Matrix&m1,const Matrix&m2);

Matrix.cpp:1:0:

C:\ Users\Peter\CLIONProjects\PK\untitled76\Matrix.h:26:79:警告:朋友声明'std :: ostream&matrixClass :: operator <<(std :: ostream&,const matrixClass :: Matrix&)'声明非模板函数[-Wnon-template-friend]朋友std :: ostream&operator <<(std :: ostream&,const Matrix&matrix);

Matrix.h:26:79:注意:

  (if this is not what you intended, make sure …
Run Code Online (Sandbox Code Playgroud)

c++ templates friend shadow operator-keyword

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

为什么std :: move()在c ++中工作?

以下是代码段:

int i=0;
int&&k=std::move(i);
Run Code Online (Sandbox Code Playgroud)

在c ++入门中,此举是

template <typename T>
typename remove_reference<T>::type &&move(T&& t)
{return static_cast<typename remove_reference<T>::type&&>(t);}
Run Code Online (Sandbox Code Playgroud)

据我所知,这个std::move模板会扣除一个像这样的函数

int&& move(int& t){return static_cast<int&&>(t);}
Run Code Online (Sandbox Code Playgroud)

作为比较和阐述我的问题,请考虑这样的示例:

 int test(int k){k=66;return k;}
 int k;
 int a=test(k);
Run Code Online (Sandbox Code Playgroud)

上面的代码将编译为:

int temp;//the temporary object
temp=k;
int a=temp;
Run Code Online (Sandbox Code Playgroud)

同样,我认为第一个代码片段将被编译为:

int&& temp=0;
int&& k=temp;//oop!temp is an lvalue!
Run Code Online (Sandbox Code Playgroud)

这似乎是错误的,因为它temp是一个左值,我有什么不对吗?

c++ c++11 value-categories stdmove

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

C++ 无法找到命名空间之外的函数

编译以下代码失败,因为第二个函数无法找到第一个函数,即使它位于命名空间之外。我自己无法弄清楚这个问题,到目前为止我也没有在网上找到任何答案。

测试.cpp:

#include <bits/stdc++.h>

struct myclass {};

template <typename T, typename U>
std::ostream& operator<< (std::ostream &os, const std::pair<T, U> &p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

namespace my {
    void operator<< (std::ostream os, myclass m) {
        std::cout << std::pair<int, int>(5, 4); // This is line 13.
    }
}

int main() {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器给出的错误( g++ test.cpp -O2 -o test.exe):
test.cpp:13:13: error: no match for 'operator<<' (operand types are …

c++ overloading operator-overloading name-lookup

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

如何为主模式定义注释语法?

我想为我使用的主要模式添加注释,这些模式目前不支持它们.我在网上找到的唯一例子显示了如何编写单行注释,但我需要配对分隔符.

我需要改变什么?

emacs comments dot-emacs

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

装饰者模式和C#

我试图运行在C#中下面的示例程序,我得到的输出"你要一部电脑",而不是"你得到一台电脑和一盘和一个显示器和键盘".

为什么这只发生在C#中,而不是在Java中.我java相同的代码我得到了适当的输出.

如果我调试我发现创建的对象层次结构是正确的但是调用computer.getComputer()总是去超级类而不是驱动类,这就是问题所在.

请帮我解决这个问题.

namespace DecoratorTest1
{

    public class Computer
    {
        public Computer()
        {
        }

        public  String getComputer()
        {
            return "computer";
        }
    }


    public abstract class ComponentDecorator : Computer
    {
        public abstract String getComputer();
    }

    public class Disk : ComponentDecorator
    {
        Computer computer;
        public Disk(Computer c)
        {
            computer = c;
        }


        public override String getComputer()
        {
            return computer.getComputer() + " and a disk";
        }

    }

    public class Monitor : ComponentDecorator
    {
        Computer computer;
        public Monitor(Computer c)
       {
            computer = c; …
Run Code Online (Sandbox Code Playgroud)

c# design-patterns decorator

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

带有尾随返回类型的 final、override、const 的语法

我试图覆盖一个 virtual 但也使用关键字override, finaland const,带有尾随返回类型。问题似乎出在派生类上,编译器错误(说我没有指定尾随返回类型)并没有太大帮助。代码在这里:https : //wandbox.org/permlink/zh3hD4Ukgrg6txyE

还贴在下面。我玩过不同的排序,但似乎仍然无法正确。任何帮助将不胜感激,谢谢。

#include<iostream>
using std::cout; using std::endl; using std::ostream;
//////////////////////////////////////////////
//Base stuff
class Base
{
public:
  Base(int i=2):bval(i){}
  virtual ~Base()=default;
  virtual auto debug(ostream& os=cout)const->ostream&;

private:
  int bval=0;
};

auto Base::debug(ostream& os) const->ostream&
{
  os << "bval: " << bval << endl;
  return os;
}

///////////////////////////////////////////////
//Derived stuff
class Derived : public Base
{
public:
  Derived(int i=2,int j=3):Base(i), dval(j){}
  ~Derived()=default;

  auto debug(ostream& os=cout) const override final->ostream&; // error here

private: …
Run Code Online (Sandbox Code Playgroud)

c++ syntax c++11

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

在模板类中编写友元函数声明的正确方法是什么?

我正在尝试编写自己的向量模板类,但是在编写友元函数声明时遇到了一些问题。

一开始我是这样写的:

template <typename T, typename Alloc = std::allocator<T>>
class vector {
public:
    friend bool operator==(const vector<T, Alloc>&, const vector<T, Alloc>&);
};
Run Code Online (Sandbox Code Playgroud)

但是编译器报告了一个警告,我声明了一个非模板函数。所以我把朋友声明改成了这样:

template <typename T, typename Alloc = std::allocator<T>>
class vector {
public:
    template <typename E, typename F>
    friend bool operator==(const vector<E, F>&, const vector<E, F>&);
};
Run Code Online (Sandbox Code Playgroud)

到目前为止一切都很好,但我认为仍然存在问题。如果我这样写,我会创建所有operator==将两个模板参数作为其友元函数的函数。例如,operator==(const vector<int>&, const vector<int>&)andoperator==(const vector<double>&, const vector<double>&)都是vector<int>的友元函数。

在模板类中编写友元函数的正确方法是什么?

c++ templates vector friend

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

关于使用int和static_assert的专业化

我想编写一个模板函数,只能使用2个数字(例如3和5),如果你尝试将它与其他数字一起使用,则会给出错误.

我可以这样做:

template<int x>
void f();

template<>
void f<3>()
{
   cout << "f<3>()\n";
}


template<>
void f<5>()
{
  cout << "f<5>()\n";
}
Run Code Online (Sandbox Code Playgroud)

然后我可以正常方式调用此函数:

f<3>();
f<5>();
Run Code Online (Sandbox Code Playgroud)

它编译得很好,如果我尝试错误地使用我的函数:

f<10>();
Run Code Online (Sandbox Code Playgroud)

编译器给我一个错误.

我有这个方法的2个问题:

1.-这是标准吗?我可以使用整数专门设计模板吗?

2.-我不喜欢你使用这种方法时得到的错误,因为错误并没有告诉用户他做错了什么.我更喜欢这样写:

template<int x>
void f()
{
    static_assert(false, "You are trying to use f with the wrong numbers");
}
Run Code Online (Sandbox Code Playgroud)

但这不编译.看来我的编译器(gcc 5.4.0)试图首先实例化主模板,因此它给出了(static_assert的)错误.

谢谢您的帮助.

如果您想知道我为什么要这样做是因为我正在学习如何编程微控制器.在微控制器中,你有一些只做一些事情的引脚.例如,引脚3和5是可以生成方波的引脚.如果在一个应用程序中我想生成一个方波,我想写下这样的东西:

square_wave<3>(frecuency);
Run Code Online (Sandbox Code Playgroud)

但是,如果几个月后我想在另一个使用不同微控制器的应用程序中重用这个代码(或更改它),我希望我的编译器对我说:"呃,在这个微控制器中你不能在引脚3中产生方波而5.使用引脚7和9".我认为这可以为我节省很多麻烦(或许不是,我真的不知道.我只是学习如何编程微控制器).

c++ microcontroller templates static-assert template-specialization

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

将unique_ptr <Object>作为unique_ptr <const Object>返回

我有这样的方法:

std::unique_ptr<const Stats> Table::GetStats() const {
  std::unique_ptr<Stats> result;
  // ...
  // Prepare stats. Under some conditions an exception may be thrown.
  // ...
  return result;
}
Run Code Online (Sandbox Code Playgroud)

问题是它不能编译:

错误:无法将'std :: unique_ptr'左值绑定到'std :: unique_ptr &&'

我可以使用以下旁路进行编译:

return std::unique_ptr<const Stats>(result.release());
Run Code Online (Sandbox Code Playgroud)

但这似乎有点过分.我无法理解,从C++的角度看第一段代码有什么问题?有更优雅的解决方案吗?

c++ const return unique-ptr c++11

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