0

Moving from Python to C++ and wrestling with the transition to a compiled language. I'm using Xcode but have had a an identical problem using Netbeans.

I have a very simple project, with a file called "main.cpp" which is the below:

#include <iostream>

class Planet {
    int radius, mass, grav_constant;

public:

    Planet(int radius, int mass, int grav_constant);
    int get_radius() { return radius; };
    void set_radius(int new_rad) { radius = new_rad; };

};



using namespace std;

int main()
{

    Planet x {1,2,3};

cout << x.get_radius();

    return 0;
};

I get the below error:

Showing Recent Errors Only
Check dependencies

CompileC /Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Intermediates/First.build/Debug/First.build/Objects-normal/x86_64/main.o First/main.cpp normal x86_64 c++ com.apple.compilers.llvm.clang.1_0.compiler
    cd "/Users/thomastry/Programming/My Xcode projects/Test/First"
    export LANG=en_US.US-ASCII
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -arch x86_64 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=gnu++1y -stdlib=libc++ -fmodules -gmodules -fmodules-cache-path=/Users/thomastry/Library/Developer/Xcode/DerivedData/ModuleCache -fmodules-prune-interval=86400 -fmodules-prune-after=345600 -fbuild-session-file=/Users/thomastry/Library/Developer/Xcode/DerivedData/ModuleCache/Session.modulevalidation -fmodules-validate-once-per-build-session -Wnon-modular-include-in-framework-module -Werror=non-modular-include-in-framework-module -Wno-trigraphs -fpascal-strings -O0 -fno-common -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Wdocumentation -Wunreachable-code -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wshorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DDEBUG=1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -fasm-blocks -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -mmacosx-version-min=10.12 -g -fvisibility-inlines-hidden -Wno-sign-conversion -Winfinite-recursion -Wmove -iquote /Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Intermediates/First.build/Debug/First.build/First-generated-files.hmap -I/Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Intermediates/First.build/Debug/First.build/First-own-target-headers.hmap -I/Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Intermediates/First.build/Debug/First.build/First-all-target-headers.hmap -iquote /Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Intermediates/First.build/Debug/First.build/First-project-headers.hmap -I/Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Products/Debug/include -I/Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Intermediates/First.build/Debug/First.build/DerivedSources/x86_64 -I/Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Intermediates/First.build/Debug/First.build/DerivedSources -F/Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Products/Debug -MMD -MT dependencies -MF /Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Intermediates/First.build/Debug/First.build/Objects-normal/x86_64/main.d --serialize-diagnostics /Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Intermediates/First.build/Debug/First.build/Objects-normal/x86_64/main.dia -c /Users/thomastry/Programming/My\ Xcode\ projects/Test/First/First/main.cpp -o /Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Intermediates/First.build/Debug/First.build/Objects-normal/x86_64/main.o

Ld /Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Products/Debug/First normal x86_64
    cd "/Users/thomastry/Programming/My Xcode projects/Test/First"
    export MACOSX_DEPLOYMENT_TARGET=10.12
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -L/Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Products/Debug -F/Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Products/Debug -filelist /Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Intermediates/First.build/Debug/First.build/Objects-normal/x86_64/First.LinkFileList -mmacosx-version-min=10.12 -Xlinker -object_path_lto -Xlinker /Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Intermediates/First.build/Debug/First.build/Objects-normal/x86_64/First_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Intermediates/First.build/Debug/First.build/Objects-normal/x86_64/First_dependency_info.dat -o /Users/thomastry/Library/Developer/Xcode/DerivedData/First-aumojcbhbilmlehavktorbosazht/Build/Products/Debug/First

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



Activity Log Complete    25/02/2017, 16:03

I have tried changing various architecture settings in the "Build Settings" menu, tried changing the linker but nothing seems to make any difference. Grateful for any suggestions.

Tom

Solved by @Biffen, Planet was not declared:

class Planet {
    int radius, mass, grav_constant;

public:

    Planet(int x, int y, int z) {
        radius = x;
        mass = y;
        grav_constant = z;

    };

    int get_radius() { return radius; };
    void set_radius(int new_rad) { radius = new_rad; };

};
TomTry
  • 77
  • 1
  • 7
  • Where is the definition of `Planet::Planet(int radius, int mass, int grav_constant)`? – Biffen Feb 25 '17 at 16:09
  • @Biffen, is that not the constructor that the error message is referring to? – TomTry Feb 25 '17 at 16:15
  • 1
    Yeah, exactly, so where's the definition? – Biffen Feb 25 '17 at 16:17
  • Thanks, stupid error from me. Have changed to: class Planet { int radius, mass, grav_constant; public: Planet(int x, int y, int z) { radius = x; mass = y; grav_constant = z; }; int get_radius() { return radius; }; void set_radius(int new_rad) { radius = new_rad; }; }; Thanks for pointing this out. – TomTry Feb 25 '17 at 16:22

0 Answers0