'Why do I get "This file does not belong to any project target, code insight features might not work properly"?
I'm facing with the linker error for the last several times I wrote something in C++. I couldn't get the hang of what it's about, from the other entries that I saw on StackOverflow. How do you think I can solve this problem? Could you please explain why I get this error and how I can avoid it in the future?
This time, I have 3 files: 1 source file, 1 class file, 1 header file. My code is like the following:
In Student class:
(where I get the warning: "This file does not belong to any project target, code insight features might not work properly.")
#include "Student.h"
#include <iostream>
Student::Student() {
name = "No-Name";
id = -1;
for (int i = 0; i < 3; i++)
classesTaken[i] = "Not-Set";
}
Student::Student(std::string n, int i, std::string courses[]) {
name = n;
id = i;
for (int j = 0; j < 3; j++)
if (courses[j] != "")
addClassTaken(courses[j]);
}
void Student::displayProfile() {
std::cout << "Name: " << name << endl;
std::cout << "Student ID: " << id << endl;
std::cout << "Classes taken: ";
for (std::string course : classesTaken)
if (course != "Not-Set")
std::cout << course << endl;
}
void Student::addClassTaken(std::string course) {
if (!checkClassIsTaken(course) && )
for (int i = 0; i < 3; i++)
if (classesTaken[i] == "Not-Set" || classesTaken[i] == "") {
classesTaken[i] = course;
break;
}
}
bool Student::checkClassIsTaken(std::string course) {
for (int i = 0; i < 3; i++)
if (classesTaken[i] == course)
return true;
return false;
}
In header file:
#pragma once
#include <string>
class Student {
std::string name;
int id;
std::string classesTaken[3];
public:
Student();
Student(std::string, int, std::string);
void displayProfile();
void addClassTaken(std::string);
bool checkClassIsTaken(std::string);
};
In source file:
#include "Student.h"
int main() {
Student student("Ece", 358, {"Math", "Sociology"});
student.displayProfile();
student.addClassTaken("Science");
student.displayProfile();
return 0;
}
And I get the following message:
[ 50%] Linking CXX executable Student Undefined symbols for architecture x86_64:
"Student::addClassTaken(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >)", referenced from: _main in main.cpp.o
"Student::displayProfile()", referenced from: _main in main.cpp.o
"Student::Student(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, int, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >)", referenced from: _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [Student] Error 1
make[2]: *** [CMakeFiles/Student.dir/all] Error 2
make[1]: *** [CMakeFiles/Student.dir/rule] Error 2
make: *** [Student] Error 2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
