0

I'm trying to test out some of the # methods of cpp (define, if, include etc). However, my compiler is running into issues when I try to write these in code. For some reason, the code

#include <iostream>

#define DEBUG 1

#if DEBUG
    #define X(x) std::cout << x << std::endl;
#else
    #define X(x) 
#endif

int main(){
    X("hi")
    X("37")
    std::cout << "Hello" << std::endl;
    

    return 0;
}

issues a compile time error that says "expected ';' at the end of declaration" pointing to the line in #endif. I've searched for solutions online, but I'm not sure where a semicolon is needed, if at all. I also get similar errors when I try to define a vector:

#include <vector>
int main(){
    vector<string> msg = {"hi", "bye"};
    return 0;
}

Is this a compiler issue, or am I missing semicolons somewhere? As for the vector code, I believe the compiler states that the semicolon is missing after variable "msg" (not at the end of the line). Thanks for any help!

rjc810
  • 425
  • 1
  • 3
  • 17
  • 1
    [Works here.](https://gcc.godbolt.org/z/nx651z) – chris Dec 24 '20 at 18:27
  • Damn, it must be my compiler. Thanks for checking. Is there anything I can do to fix this? clang++ helloworld.cpp compiles without errors, but vscode is not building my code. – rjc810 Dec 24 '20 at 18:32
  • Check which specific compiler and version vscode is finding and using. – chris Dec 24 '20 at 18:35
  • It's using clang++, but I'm not sure what version. terminal says the only one ive downloaded is 12.0.0 – rjc810 Dec 24 '20 at 18:36
  • Are these errors when you build or part of instant feedback? – chris Dec 24 '20 at 18:38
  • Update: it compiles, but now I'm getting this error ` Loaded '/usr/lib/system/libxpc.dylib'. Symbols loaded. Loaded '/usr/lib/libobjc.A.dylib'. Symbols loaded. Loaded '/usr/lib/liboah.dylib'. Symbols loaded. @"hi\r\n" @"37\r\n" @"Hello\r\n" The program '/Users/rjc/projects/helloworld/helloworld' has exited with code 0 (0x00000000). ` – rjc810 Dec 24 '20 at 18:39
  • The errors appeared as instant feedback – rjc810 Dec 24 '20 at 18:40
  • That looks like debugging console information. – chris Dec 24 '20 at 18:44
  • 1
    Side note. I would recommend to design function-like macros so that they can be used like if they were functions. Namely, in a way that forces a semi-colon after them. In `DEBUG`, `#define X(x) std::cout << x << std::endl` (without the terminal semi-colon), and `(void)0` otherwise. – prapin Dec 24 '20 at 20:50

0 Answers0