相关疑难解决方法(0)

Matrix中的'operator*'没有匹配运算符

我有一个矩阵类,如下所示:

template <size_t M, size_t N, typename T>
class Matrix
{
public:
    Matrix<M, N, T> operator +(const Matrix<M, N, T>& B) const;
    template <size_t P> Matrix<M,P,T> operator*(const Matrix<N, P, T>& B) const;
    template <typename T2> operator T2() const;  

private:
  T data[M][N];
};

// ... the body is in header file too  ...//
Run Code Online (Sandbox Code Playgroud)

身体写得很好,一切都运作良好.当我定义两个矩阵时如下:

Matrix < 10, 10, int> m1;
Matrix < 10, 10, float> m2;

m1 + m2;  // OK
m1 * m2;  // error: no match for 'operator*' in …
Run Code Online (Sandbox Code Playgroud)

c++ templates casting operators

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

这是否会导致引用超出范围后指向任何内容?

以下代码是否安全?

class B {
  public:
    int& b;
    B (int& _b) :
      b(_b) {}
};

B* foo() {
  int a;
  return new B(a);
}
Run Code Online (Sandbox Code Playgroud)

foo返回的对象中的引用是否会指向任何内容(因为int a超出范围)或编译是否计算出来?

c++ constructor scope reference

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

重载operator []以获取模板类-c ++

刚写了一个模板数组类的代码(我知道它还没有完成),并试图记住如何重载运算符(同时没有特殊的困难......).

无论如何,在思考如何实现时operator[]我想知道如果索引超出数组边界会发生什么...我很确定我不可能返回NULL(因为返回类型),对?如果是这样,如果索引超出边界,我应该返回什么?

这里是代码,大部分内容对我的问题都是多余的,但它可能会帮助谷歌运营商超载的任何人,所以我发布了完整的代码......

#ifndef __MYARRAY_H
#define __MYARRAY_H

#include <iostream>
using namespace std;

template <class T>
class MyArray
{
    int phisicalSize, logicalSize;
    char printDelimiter;
    T* arr;

public: 
    MyArray(int size=10, char printDelimiter=' ');
    MyArray(const MyArray& other);
    ~MyArray() {delete []arr;}

    const MyArray& operator=(const MyArray& other);
    const MyArray& operator+=(const T& newVal);

    T& operator[](int index);

    friend ostream& operator<<(ostream& os, const MyArray& ma)
    {
        for(int i=0; i<ma.logicalSize; i++)
            os << ma.arr[i] << ma.printDelimiter;
        return os;
    }
};

template <class T>
T& MyArray<T>::operator[]( int index …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

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

比较c ++中的int和bitset

如何将bitset与整数进行比较?或者更一般地使用整数运算符:类似这样的事情:

#include <iostream> 
#include <iomanip>
#include <bitset>
using namespace std;

int main()
{
bitset<4> _b1 = 3 ; 


if(_b1>=2 )
    cout<<_b1;



system("pause");
return 0;
Run Code Online (Sandbox Code Playgroud)

}

c++ bitset

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

访问违规阅读课堂上的位置

我是C++的新手,我一直在玩指针和类.我遇到了一个问题,到目前为止我还没有找到解决方案:

RAII.exe中0x77F87508(msvcr110d.dll)的未处理异常:0xC0000005:访问冲突读取位置0xCCCCCCC0.

它似乎与我访问一个我无法访问的指针有关.

Main.cpp的:

#include <memory>
#include <iostream>
#include "Example.hpp"

void example()
{
    Example e;
}

int main()
{
    example();
    std::cout << "Press any key to exit";
    std::cin.get();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Example.cpp:

#include "Example.hpp"

Example::Example()
{
    m_a = new int(1);
    m_b = new int(2);
    m_b = new int(3);
}

Example::~Example()
{
    delete m_a;
    delete m_b;
    delete m_c;
}
Run Code Online (Sandbox Code Playgroud)

Example.hpp:

#ifndef _EXAMPLE_HPP_
#define _EXAMPLE_HPP_

#include <memory>
#include <iostream>

class Example
{
private:
    int *m_a;
    int *m_b;
    int *m_c;
public:
    Example();
    ~Example(); …
Run Code Online (Sandbox Code Playgroud)

c++ memory runtime-error

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

理解_countof宏

template <typename _CountofType, size_t _SizeOfArray>
char( *__countof_helper1( _CountofType(&_Array)[_SizeOfArray]))[_SizeOfArray];

#define _myCountOf(_Array) (sizeof(*__countof_helper1(_Array)) + 0)
Run Code Online (Sandbox Code Playgroud)

我试图理解 _countof 宏,但无法理解它如何能够计算数组的大小。请有人一点一点解释上面的代码

c++ templates

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

指针声明传染媒介

看看我教授给我的一些代码,我不明白发生了什么.我是编程新手,完全迷失了.

vector <_Account*>*myvector = nullptr;
Run Code Online (Sandbox Code Playgroud)

所以我知道他创建了一个向量,我知道一个现有的类,Account所以这是一个vector指向Account对象的指针吗?我不知道第二个星号是做什么的?

c++ pointers vector nullptr

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

C++ Friend函数不访问私有成员

所以我正在尝试制作一个国际象棋游戏(对我来说是一个终极挑战),而我是这部分的残障......

所以我创建了一个片段对象,其思想是在主游戏代码中,我有一个片段数组,我将数组的地址传递给函数"InitilisePieces"和团队(黑色或白色),它将分配所有的碎片.所以我让这个函数成为了访问所有私有成员的朋友,并且它出现了一个错误,说"无法访问",我不明白我做的事情有什么问题.任何帮助将不仅仅是赞赏!

侧注:像State_和_Location这样的东西,以及正确定义的结构和枚举等,不是问题...(我不认为)

头文件包含:

class   __Piece
{
private:

    State_              e_state;
    Piece_Type_         e_type;
    Team_               e_team;
    _Location           st_location;

    friend void         InitilisePieces     ( __Piece(*)[16], Team_);

public:
    __Piece             ();

};
Run Code Online (Sandbox Code Playgroud)

.cpp文件包含:

void                    InitilisePieces     ( __Piece * pao_piece[16], Team_ )
{
    int         n_count;

    for ( n_count = 0; n_count < 16; n_count++ )
    {
        pao_piece[ n_count ]->e_state;
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:

谢谢你的解释,我得到了我现在出错的地方......那么传递__Piece数组地址的参数是什么?

c++ arrays object friend

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

队列不是模板

我一直在使用C ++ STL创建代码。我想使用“队列”。因此,我编写了如下代码。但是,我遇到了“队列不是模板”错误。如您所见,我在“ Common.h”文件中编写了与队列(iostream,queue)相关的标头,并在“ DataQueue.h”文件中包含了“ Common.h”。但是,VS2013 IDE工具说“ queue m_deQueue”是错误的,因为queue不是模板。我不知道为什么..发生此错误。任何帮助表示赞赏!

//[Common.h]
#ifndef _COMMON_
#define _COMMON_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

//thread related headers
#include <Windows.h>
#include <process.h>

//socket related headers
#include <winsock.h>

#include <iostream>
#include <queue>
#include <deque>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

#endif


//[DataQueue.h]
#ifndef _QUEUE_
#define _QUEUE_

#include "SocketStruct.h"
#include "Common.h"

class CDataQueue{
private:
    static CDataQueue*                  m_cQueue;
//  deque <ST_MONITORING_RESULT>        m_deQueue;
    queue <ST_MONITORING_RESULT>        m_deQueue;
    CRITICAL_SECTION                    m_stCriticalSection;

    CDataQueue();
    ~CDataQueue();

public:
    static CDataQueue* getDataQueue(){ …
Run Code Online (Sandbox Code Playgroud)

c++ queue stl

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

执行字符串赋值时C++程序崩溃

我有一个奇怪的问题,这是我的代码:

test.h

#ifndef _test_
#define _test_
#include <iostream>
#include <string>
class Test {
public:
    Test();
    ~Test();
    void addName(std::string _Name);
private:
    std::string Name;
};
#endif // _test_ 
Run Code Online (Sandbox Code Playgroud)

TEST.CPP

#include "test.h"
Test::Test() {}
Test::~Test() {}
void Test::addName(std::string _Name) {
    std::cout << _Name << std::endl;
    Name = _Name;
    std::cout << _Name << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include "test.h"
int main(int argc, char* argv[]) {
    Test* project;
    project->addName("abc");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

结果:

ABC

该计划意外完成.

c++ string class

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