1

I want to concat the constant string and the macro defined string.

#define DEEP_DRIVE_NET "C:/Users/tumh/hookv/deep_drive_model.prototxt"
#define DEEP_DRIVE_WEIGHT "C:/Users/tumh/hookv/caffe_deep_drive_train_iter_35352.caffemodel"
CHECK(file_exist(DEEP_DRIVE_WEIGHT)) << "Net Weight " + DEEP_DRIVE_WEIGHT + " Not Found";
CHECK(file_exist(DEEP_DRIVE_NET)) << "Net definition " + DEEP_DRIVE_NET + " Not Found";

the compile error from msvc 2013 compiler is

C:\Users\tumh\hookv\samples\Test\Inference.cpp(28): error C2110: '+' : cannot add two pointers [C:\Users\tumh\hookv\build\NativeTrainer.vcxproj]
C:\Users\tumh\hookv\samples\Test\Inference.cpp(29): error C2110: '+' : cannot add two pointers [C:\Users\tumh\hookv\build\NativeTrainer.vcxproj]

How can I concatenate such strings?

Thanks.

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
alec.tu
  • 1,647
  • 2
  • 20
  • 41
  • Better use either c or c++ tags.. Probably a duplicate, e.g. http://stackoverflow.com/questions/5256313/c-c-macro-string-concatenation#5256426 http://stackoverflow.com/questions/1739102/concatenating-strings-in-macros-c – stijn Dec 02 '16 at 15:15

1 Answers1

3

Just omit the + operations to concatenate c-style string literals:

CHECK(file_exist(DEEP_DRIVE_WEIGHT)) << "Net Weight " DEEP_DRIVE_WEIGHT " Not Found";
CHECK(file_exist(DEEP_DRIVE_NET)) << "Net definition " DEEP_DRIVE_NET " Not Found";
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • I do not understand why this can work. The pre-processor would translate into `"Net Weight " "C:/Users/tumh/hookv/deep_drive_model.prototxt" " Not Found"` , am I right? – alec.tu Dec 02 '16 at 15:20
  • @alec.tu: C string literals are concatenated automagically if delimited by white spaces only. – alk Dec 02 '16 at 15:21
  • Understood. Can you explain what is "pointer" in this case? two pointers which point to two constant string "Net Weight" and "Not Found"? – alec.tu Dec 02 '16 at 15:22
  • @alec.tu A `"xxxxx"` literal has the type of `const char[]` so it constitutes a pointer finally. – πάντα ῥεῖ Dec 02 '16 at 15:24