1

My project setting is

  • Xcode 8.3 (8E162)
  • Language C++

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

1 Answer 1

1

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?";
}
Sign up to request clarification or add additional context in comments.

4 Comments

It build success, but can't see (can u see me?)
strange. Either add std::cout << std::endl or a call to std::cout.flush() somewhere before returning from the main function
If your trouble is resolved, please accept the answer
stack overflow says, wait 2 min. sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.