0

I'm running VScode on an M1 Mac. I keep getting this error whenever I try to run my code:

Undefined symbols for architecture arm64:
  "Array::Array(int)", referenced from:
      _main in Main-528956.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is a pretty simple project for a c++ class but this error is out of my league. Here are the three classes I have to use:

Array.h :

#ifndef _ARRAY_H_
#define _ARRAY_H_

    class Array
    {
    public:
        Array();
        Array(int);
    private:
        int size;
        int *arr;
    };
    #endif

Array.cpp :

#include <iostream>
#include "Array.h"

using namespace std;

Array::Array()
{
    size = 0;
    arr = nullptr;
}

Array::Array(int s)
{
    size = s;
    arr = new int[size];
    for (int i = 0; i < size; i++)
    {
        arr[i] = i;
    }
    cout << "overloaded constructor works" << endl;
    delete[] arr;
}

main.cpp :

#include <iostream>
#include "Array.h"

using namespace std;

int main()
{

    Array arr1(5), arr2(10);
    return 0;
}

Let me know if I need to add any other information.

s.eyal
  • 21
  • 4
  • Look at your build console and you'll see Array.cpp isn't being built (and thus obviously also not linked). You may find [this answer](https://stackoverflow.com/a/59236875/1322972) helpful until such time as you start using Makefiles, or better still CMake (do it... you know you want to... all the cool kids are doing it... we'll even throw in the first one for free). – WhozCraig Feb 23 '22 at 08:52
  • The program works without errors in Visual Studio, if you must use VS Code please refer to WhozCraig's suggestion. – Yujian Yao - MSFT Feb 24 '22 at 02:14

0 Answers0