【问题标题】:Template and vector errors模板和矢量错误
【发布时间】:2011-12-06 20:18:55
【问题描述】:

我不知道我做错了什么这是我第一次分离 .cpp 文件并使用模板和向量。我不断收到这两个错误:错误 C2143:语法错误:在 '

main.cpp

#include <iostream>
#include <vector>
using namespace std;

template <typename T>
void write_vector(const vector<T>& V);

int main()
{
    int n;
    int value;
    vector<int> V;
    cout << "Enter n: ";
    cin >> n;
    cout << "Enter " << n << " integer values: ";
    while(V.size() < n && cin >> value){
        V.push_back(value);
    }

    write_vector(V);

    return 0;
}

writeVector.cpp

template <typename T>
void write_vector(const vector<T> &V)
{
    for(int i=0; i < V.size(); i++)
        cout << V[i] << " ";
}

【问题讨论】:

  • 您是#include 还是显式实例化该函数?你的问题缺乏细节。
  • 在哪里包含 writeVector.cpp?- 它最终应该包含在 main.cpp 中,因为模板不能单独编译为目标文件。您在哪一行收到错误?

标签: c++ file templates vector header-files


【解决方案1】:

您需要显式实例化来执行此操作,因为编译器在编译 .cpp 时不知道模板需要针对哪些类型进行编译。见Explicit instantiation - when is it used?

【讨论】:

  • 那么我的 .h 和 .cpp 文件会是什么样子。
【解决方案2】:

您的模板函数必须预先定义,以便编译器可以使用它。要么在单个文件中的 main() 之前定义它,要么在头文件中 #include 它。

总的来说,模板函数应该在头文件中定义。查看Why can templates only be implemented in the header file?的答案

【讨论】:

  • 我的模板不是在main之前定义的吗?
  • 编译器只知道存在一个模板类型。您需要将定义移动到头文件并在 main 之前将其#include 或将其复制到 main 上方。
【解决方案3】:

据我所知,您不能将模板的声明和实现分开。

【讨论】:

    【解决方案4】:

    这两个文件中都需要 includes 和 using。此时,您可能需要其他答案中提到的显式实例化,但这些会显示为链接器问题。

    【讨论】:

      【解决方案5】:

      write_vector 需要在头文件 (writeVector.h) 中而不是 .cpp 文件中,如果您想在不同模块之间共享它。有关您为什么需要这样做的更多信息,请参阅C++ FAQ

      我认为您的语法错误是因为您在 writeVector.cpp 中缺少using namespace std;?当你将它移动到头文件时,你不应该把这个 using 指令放在那里,因为只要包含这个文件,就会不必要地污染全局命名空间。相反,您应该明确地将所有对vector 的引用限定为std::vector

      【讨论】:

        【解决方案6】:

        在你的main.cpp上面加上:

        #include "Vector.cpp" // since you are learning, this is really strange and should be avoided, but for your example we leave it as is.
        

        在你的Vector.cpp

        #include <vector>
        #include <iostream>
        
        template<class T>
        void write_vector(const std::vector<T>& V)
        {
            for(int i=0; i < V.size(); i++)
                std::cout << V[i] << " ";
        }
        

        不要使用 using 语句。坏习惯。您不需要显式实例化或其他东西。你的编译器可以推断出正确的模板。

        虽然这可行,但它是糟糕的代码。使用模板时,请始终将所有内容写入 .h 文件。因此,将您的 Vector.cpp 更改为 Vector.h。从你的 main.cpp 和 #include "Vector.cpp" 中删除前向声明

        您的代码无需任何其他更改即可正常工作。

        最后,C++ 的做事方式将是例如:

        #include <vector>
        #include <iostream>
        #include <algorithm>
        
        template<class T>
        void write_vector(const std::vector<T>& V)
        {
            std::copy(V.begin(), V.end(), std::ostream_iterator<T>(std::cout, " ")); // you don't even need this function right?
            //for(int i=0; i < V.size(); i++)
         //       std::cout << V[i] << " ";
        }
        

        你可以为 cin 做类似的事情。

        【讨论】:

          猜你喜欢
          • 2015-06-27
          • 2011-07-16
          • 2013-05-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多