c++ - main.obj : error LNK2019: unresolved externa

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?

(34个答案)


5年前关闭。




我知道这个特殊错误已经被提出了十二次了,但是通过我的所有搜索,我一直没有找到与我的具体情况相关的解决方案。

我是一名从Java学习C++的大学生。我知道使用 vector 而不是数组会更好。我也知道我可能理解错误的指针,希望您的解释能在这方面为我提供帮助。

好的,这是我的三个文件:

main.cpp
#include <iostream>
#include "Arrays.h"

// Global variables
const int ARRAY_SIZE = 4;
const int MIN_RANGE = 0;
const int MAX_RANGE = 100;

// Main function
int main()
{
    // Create two arrays populated with random numbers
    int* Array1 = createArray(ARRAY_SIZE, MIN_RANGE, MAX_RANGE); // One of these days I'll understand why ponters are so cool
    int* Array2 = createArray(ARRAY_SIZE, MIN_RANGE, MAX_RANGE); // Right now I find them a tad irritating

    // Create the third array
    int* Array3 = mergeArrays(Array1, Array2);

    // Print all arrays with the format "array1[1] + array2[1] = array3[1]"
    for (size_t i = 0; i < ARRAY_SIZE; i++)
    {
        std::cout << Array1[i] << " + " << Array2[i] << " = " << Array3[i] << std::endl << std::endl;
    }

    // Wait for input...
    system("pause");

    return 0;
}

数组
// Fucntion Prototypes
int* createArray(int arraySize, int minRange, int maxRange); // Creates an array populated with random numbers
int* mergeArrays(int* firstArray, int* secondArray); // Creates an array by adding the associated eements of two arrays
void printHeader(); // Prints the program's header

Arrays.cpp
#include <cstdlib>
#include <ctime>
#include "Arrays.h"

// Function to create an array with n random numbers between 0 and x
int* createArray(int arraySize, int minRange, int maxRange)
{
    srand(time(NULL)); // Generate seed for rand()

    int* Array = new int[arraySize]; // Create a pointer to a new array

    for (size_t i = 0; i < arraySize; i++) // Loop to assign values to each index in the array
    {
        // Generate a random number for each index in the array
        Array[i] = (rand() % maxRange) + minRange;
    }

    return Array;
}

// Function to create an array from two arrays with the associated values added together
int* mergeArrays(int* firstArray, int* secondArray, int arraySize)
{
    int* resultArray = new int[arraySize]; // Create a pointer to a new array

    for (size_t i = 0; i < arraySize; i++) // Loop to assign values to each index in the array
    {
        /* The value at each index in the array is equal to the sum of the values
        at the same index in the other two arrays*/
        resultArray[i] = firstArray[i] + secondArray[i];
    }

    return resultArray;
}

在Visual Studio 2013中构建该错误时,出现的错误是:
1>main.obj : error LNK2019: unresolved external symbol "int * __cdecl mergeArrays(int *,int *)" (?mergeArrays@@YAPAHPAH0@Z) referenced in function _main
1>C:\Assignment 2_1\Debug\Assignment 2_1.exe : fatal error LNK1120: 1 unresolved externals

最佳答案

声明和实现之间有区别。

int* mergeArrays(int* firstArray, int* secondArray) //.h
int* mergeArrays(int* firstArray, int* secondArray, int arraySize) //.cpp

您的主要功能使用.h文件中的功能,但是,此功能从未实现。
在cpp文件中,您声明一个具有相同名称但另一个签名(重载)的函数

关于c++ - main.obj : error LNK2019: unresolved external symbol in C++ [duplicate],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28709673/

上一篇:c++ - Visual Studio编译错误查找文件

下一篇:c++ - 错误编译 Unresolved external 错误

相关文章:

c++ - 如何过滤或 "grep"C++ vector ?

c++ - VS 2008 C++ 构建输出?

c++ - 将字符传递给其他函数的最佳实践

c++ - 函数原型(prototype)错误,非空函数

swift - 如何从自定义错误访问Swift属性?

c++ - 如何在C++中正确使用#ifndef#define #endif

c++ - 对于输入迭代器,为什么 a == b 并不意味着++a ==++b?

c# - 将 SAFEARRAY 从 C++ 返回到 C#

c++ - 变量名中的美元符号?

c++ - Vulkan 和 glfw - glfwVulkanSupported() 总是返回 false

相关文章:

c++ - 错误 : too many template-parameter-lists

java - vlcj 在尝试创建服务器流时出错

java - 如何调用其他类(class)的名单?

java - Unresolved 编译问题: The method parseInt(String

c++ - ISO C++禁止声明多集

java - 为什么我无法定义数组?

c++ - Visual Studio编译错误查找文件

java - Java博士;为什么不编译呢?

java - 错误 : variable data might not have been init

sql-server - 从以下脚本获取错误