c++ - 在.h头文件中用到vector需要写include<vector>语句吗?
天蓬老师
天蓬老师 2017-04-17 13:42:53
0
2
488

打算重拾C++,好久不写都忘了...请教各路大神,以下代码的问题出在哪?
以下是头文件

#pragma once
class MinimumPathSum
{
public:
    MinimumPathSum();
    ~MinimumPathSum();
    int minPathSum(vector< vector<int> > &grid);
};

以下是.cpp文件

#include "stdafx.h"
#include "MinimumPathSum.h"
#include <vector>
#include <algorithm>

using namespace std;

MinimumPathSum::MinimumPathSum()
{
}


MinimumPathSum::~MinimumPathSum()
{
}

int MinimumPathSum::minPathSum(vector<vector<int> > &grid)
{
    // write your code here
    int f[1000][1000];
    if (grid.size() == 0 || grid[0].size() == 0)
        return 0;
    f[0][0] = grid[0][0];
    for (int i = 1; i < grid.size(); i++)
        f[i][0] = f[i - 1][0] + grid[i][0];
    for (int i = 1; i < grid[0].size(); i++)
        f[0][i] = f[0][i - 1] + grid[0][i];
    for (int i = 1; i < grid.size(); i++)
        for (int j = 1; j < grid[0].size(); j++)
            f[i][j] = min(f[i - 1][j], f[i][j - 1]) + grid[i][j];

    return f[grid.size() - 1][grid[0].size() - 1];
}

以下是测试算法的代码

// minimum-path-sum.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "MinimumPathSum.h"
#include <vector>
#include <iostream>

using namespace std;
int main()
{
    vector<vector<int> > v(3,vector<int>(3));
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3;j++)
        {
            v[i][j] = i*j;
        }
    }
    MinimumPathSum mps;
    int result=mps.minPathSum(v);
    cout << result << endl;
    return 0;
}

以下是错误信息

代码    说明
C2061    语法错误: 标识符“vector”
代码    说明
C2660    “MinimumPathSum::minPathSum”: 函数不接受 1 个参数
代码    说明
C2511    “int MinimumPathSum::minPathSum(std::vector<std::vector<int,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>> &)”:“MinimumPathSum”中没有找到重载的成员函数

我的问题是:
1.在头文件中需要include语句吗?
2.为什么上面的代码会提示不接受一个参数?

先谢谢各位大神了

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
伊谢尔伦

Let’s talk about the conclusion first: There are two habits regarding header file references. One is to write all used libraries in the header file, and the other is to write the used libraries in the implementation file. I personally prefer the former approach.

The question is that the identifier vector cannot be found during compilation. There are two solutions:

Method 1:

  • Add MinimumPathSum.h to #include <vector>;

  • Change the .h in vector to std::vector.

Method 2:

  • Modify all references to MinimumPathSum.h and quote <vector> before quoting this header file. That is, change the order of the first few lines between the two .cpp as follows:

    //#include "MinimumPathSum.h"    // <-- 下移
    #include <vector>
    #include <iostream>
     
    using namespace std;
    #include "MinimumPathSum.h"      // <-- 到这里

    It will compile and run normally.

Method 2 (modification):

  • Change the MinimumPathSum.h in vector to std::vector;

  • Modify all references to MinimumPathSum.h and quote <vector> before quoting this header file. That is, change the order of the first few lines between the two .cpp as follows:

    //#include "MinimumPathSum.h"    // <-- 下移
    #include <vector>
    #include "MinimumPathSum.h"      // <-- 到这里
    #include <iostream>
    
    using namespace std;

Please refer to this article for more introduction.

伊谢尔伦

Of course, the definition in .h needs to be a known type. Except for pointers, which can be declared directly without checking, they must be clearly defined
Once you solve this error, the error will disappear

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template