vendredi 26 mai 2017

clang: error: linker command failed on vscode. [duplicate]

I got the following error. How can I fix it? It's my first time to compile multiple sources on vscode.

Error message

Undefined symbols for architecture x86_64:
  "Vector::size()", referenced from:
      sqrt_sum(Vector&) in user-9f738c.o
  "Vector::operator[](int)", referenced from:
      sqrt_sum(Vector&) in user-9f738c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

user.cpp

#include "Vector.h"
#include <cmath>

using namespace std;

double sqrt_sum(Vector& v)
{
    double sum = 0;
    for (int i = 0; i != v.size(); ++i)
        sum += sqrt(v[i]);
    return sum;
}

int main() {

}

Vector.h

class Vector
{
    public:
        Vector(int s);
        double& operator[](int i);
        int size();
    private:
        double* elem;
        int sz;
};

Vector.cpp

#include "Vector.h"

Vector::Vector(int s):elem {new double[s]}, sz{s} 
{
}

double& Vector::operator[](int i)
{
    return elem[i];
}

int Vector::size()
{
    return sz;
}

tasks.json

{
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "args": ["-std=c++11", "-O2", "-g", "user.cpp"],
    "showOutput": "always"
}

Aucun commentaire:

Enregistrer un commentaire