首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >vs10编译器这是个bug吗?

vs10编译器这是个bug吗?
EN

Stack Overflow用户
提问于 2012-09-30 00:22:10
回答 1查看 155关注 0票数 0

Mat2D.h

代码语言:javascript
运行
复制
#pragma once

#include <iostream>
#include <iomanip>
#include <assert.h>

namespace Ggicci
{
    template<class T>
    class Mat2D
    {
    private:
        int _rows;
        int _cols;
        T** _data;

    public:
        Mat2D()
            :_rows(0), _cols(0), _data(NULL)
        {

        }

        Mat2D(const Mat2D<T>& rhs)
            :_rows(rhs._rows), _cols(rhs._cols)
        {   
            allocateData();
            cloneData(rhs._data);
        }

        Mat2D(int rows, int cols)
        {   
            initSize(rows, cols);
            allocateData();
            all(0);
        }

        Mat2D(int rows, int cols, T initValue)      
        {
            initSize(rows, cols);
            allocateData();
            all(initValue);
        }

        Mat2D(int rows, int cols, const T* data)
        {   
            initSize(rows, cols);
            allocateData();
            assignData(data);
        }

        ~Mat2D()
        {   
            destroyData();
        }

        const Mat2D& operator = (const Mat2D<T>& rhs)
        {
            if (this == &rhs)
            {
                return *this;
            }
            this->resize(rhs._rows, rhs._cols);
            this->cloneData(rhs._data);
            return *this;
        }

        T& operator() (int row, int col)
        {
            assert(row >= 0 && row < _rows && col >= 0 && col < _cols);
            return _data[row][col];
        }

        T operator() (int row, int col) const
        {
            assert(row >= 0 && row < _rows && col >= 0 && col < _cols);
            return _data[row][col];
        }

        Mat2D<T> operator * (const Mat2D<T>& rhs)
        {   
            assert(this->_cols == rhs._rows);       
            int new_rows = this->_rows;
            int new_cols = rhs._cols;
            Mat2D<T> result(new_rows, new_cols);
            for (int i = 0; i < new_rows; i++)
            {
                for (int j = 0; j < new_cols; j++)
                {
                    T sum = 0;
                    for (int k = 0; k < this->_cols; k++)
                    {
                        sum += this->_data[i][k] * rhs._data[k][j];
                    }
                    result._data[i][j] = sum;
                }
            }
            return result;
        }

        void resize(int rows, int cols)
        {
            destroyData();
            initSize(rows, cols);
            allocateData();
            all(0);
        }

        void all(T value)
        {
            for (int i = 0; i < _rows; i++)
                for (int j = 0; j < _cols; j++)
                    _data[i][j] = value;
        }

    private:
        void initSize(int rows, int cols)
        {
            assert(rows > 0 && cols > 0 && rows < 65536 && cols < 65536);
            _rows = rows;
            _cols = cols;
        }

        void allocateData()
        {
            _data = new T*[_rows];
            for(int i = 0; i < _rows; i++)
            {
                _data[i] = new T[_cols];
            }       
        }

        void assignData(const T* data)
        {   
            for (int i = 0; i < _rows; i++)
            {
                for (int j = 0; j < _cols; j++)
                {   
                    _data[i][j] = data[i*_rows + j];
                }
            }
        }

        void cloneData(T** data)
        {   
            for (int i = 0; i < _rows; i++)
                for (int j = 0; j < _cols; j++)
                    _data[i][j] = data[i][j];
        }

        void destroyData()
        {
            for(int i = 0; i < _rows; i++)
                delete _data[i];
            delete _data;
        }

        /*template<class T>*/  //--> Line 158
        friend std::ostream& operator << (std::ostream& out, const Mat2D<T>& rhs)
        {
            for(int i = 0; i < rhs._rows; i++)
            {
                for(int j = 0; j < rhs._cols; j++)
                {
                    out << std::setw(12) << rhs._data[i][j];
                }
                out << std::endl;
            }
            return out;
        }
    };
}

main.cpp

代码语言:javascript
运行
复制
#include <iostream>
#include <string>
#include <cmath>
#include "Mat2D.h"
using namespace std;
using namespace Ggicci;

Mat2D<float> getRotateMatrix33f(float theta, const Mat2D<float>& ref_point = Mat2D<float>(1, 2, 0.0f))
{
    theta = theta / 180 * 3.1415f;
    float rotate[] = {
        cos(theta), -sin(theta),    0,
        sin(theta), cos(theta),     0,
        0,          0,              1
    };
    Mat2D<float> result(3, 3, rotate);
    if (ref_point(0, 0) != 0 && ref_point(0, 1) != 0)
    {
        float dx = ref_point(0, 0);
        float dy = ref_point(0, 1);
        float translate_data[] = {
            1,  0,  0,
            0,  1,  0,
            dx, dy, 1
        };
        float translate_inv_data[] = {
            1,      0,      0,
            0,      1,      0,
            -dx,    -dy,    1
        };
        Mat2D<float> translate(3, 3, translate_data);
        Mat2D<float> translate_inv(3, 3, translate_inv_data);
        result = translate * result * translate_inv;
    }
    return result;
}

int main()
{
    int data_a[] = {1, 2};
    int data_b[] = {2, 3, 4, 5, 6, 7, 8, 9};
    Mat2D<int> a(1, 2, data_a);
    Mat2D<int> b(2, 4, data_b); 
    Mat2D<int> c;
    c = a * b;
    cout << "c = " << endl << c << endl;
    cout << "rotate matrix: " << endl << getRotateMatrix33f(30.0f) << endl;
    return 0;
}

如果我在Mat2D.h中注释line 158,那么一切都是正常的,项目可以成功构建并运行,尽管出现了4个错误:

如果我不评论line 158

代码语言:javascript
运行
复制
 error C2995: 'std::ostream &Ggicci::operator <<(std::ostream &,const Ggicci::Mat2D<T>
 &)' : function template has already been defined. 

然后,我在main中注释了函数getRotateMatrix33f及其调用,项目重新构建并成功运行,没有错误。那么发生了什么呢?它把我搞糊涂了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-30 00:32:53

在注释了第158行之后实际上没有错误,但是MSVC仍然会想起你旧代码或者可能在解析你的代码时出错,C++团队承认这是他们的intellisense引擎中的一个错误,但是他们说他们直到MSVC2012才能修复它,所以如果你可以用那个版本测试你的代码

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12654337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档