stories2
stories2

Reputation: 486

Linker command failed with exit code 1 C++

My project setting is

My project structure is

├── README.md
├── TrollLanguage
│   ├── TokenParser.cpp
│   ├── TokenParser.hpp
│   └── main.cpp

My source code is

main.cpp

#include <iostream>
#include "TokenParser.hpp"

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    TokenParser *tokenParser = new TokenParser();
    tokenParser->TestFunc();
    return 0;
}

TokenParser.cpp

#include "TokenParser.hpp"

void TestFunc() {
    std::cout << "can u see me?";
}
enter code here

TokenParser.hpp

#ifndef TokenParser_hpp
#define TokenParser_hpp

#include <iostream>

class TokenParser {
public:
    void TestFunc();
};

#endif /* TokenParser_hpp */

Expected behavior is

Hello, World!
can u see me?

Actual behavior is

Apple Mach-O Linker (Id) Error
"TokenParser::TestFunc()", referenced from:
Linker command failed with exit code 1(use -v to see invocation

Upvotes: 1

Views: 651

Answers (1)

apmccartney
apmccartney

Reputation: 743

You need to prepend the class name as part of the function definition in the implementation file. As it stands, you're defining a free function called TestFunc.

#include "TokenParser.hpp"

void TokenParser::TestFunc() {
    std::cout << "can u see me?";
}

Upvotes: 1

Related Questions