0

I'm developing a webserver using C++ and I want a string array to contain the HTML code of my HTML elements.

In the definition of that array, I want to supplement hard-coded characters with strings that are defined in other arrays.

For example, my webpages[] array would contain the HTML for each webpage and be defined as:

string webpages[NUM_WEBPAGES] =
{
  ...
  "<p>Please <a href=\"" + urls[PAGE_LOGIN] + "\">login here</a></p>",
  ...
};

However, the HTML that gets sent to the client is:

<p>Please <a href="">login here</a></p>

As you can see, the url from urls[PAGE_LOGIN] was not populated into the HTML.

I have confirmed via code inspection and gdb that urls[PAGE_LOGIN] has the value /login.

(1) Why is this not working?

(2) Is this possible?

(3) If no, any recommendations?

Thank you!

EDIT: Minimal reproducible code example... because I don't know how to stackoverflow.

Also my C++ is bad.

webpages.h:
enum webpage
{
  PAGE_LOGIN,
  NUM_WEBPAGES
};

extern string webpages[NUM_WEBPAGES];

webpages.cpp:
string webpages =
{
  "<p>Please <a href=\"" + urls[PAGE_LOGIN] + "\">login here</a></p>",
};

urls.h:
extern string urls[NUM_WEBPAGES];

urls.cpp:
string urls[NUM_WEBPAGES] =
{
  "/login"
};
Ashley
  • 13
  • 3
  • Why not simply use a loop and populate each entry in `webpages`? You're trying "fancy stuff" that doesn't exist in C++. – PaulMcKenzie Oct 13 '19 at 00:47
  • using the debugger, verify the contents of that element (webpages[x]). – jdigital Oct 13 '19 at 00:50
  • if you're doing something that c++ doesn't support, you're likely to get a compiler warning or error. did you notice any compiler messages? – jdigital Oct 13 '19 at 00:51
  • 1
    What type is `urls`? In fact, can you please post a [repro]? It is unclear how all the variables are defined. – walnut Oct 13 '19 at 00:52
  • @jdigital: The debugger prints "

    Please login here

    " No compiler warnings. Compiling with -Wall.
    – Ashley Oct 13 '19 at 00:56
  • 1
    as suggested above, please create a minimal reproducible example. there's something you're not showing us. by paring this down to the smallest possible repro, it will make it easier for us to help (or even better, it might help you realize where the problem lies). – jdigital Oct 13 '19 at 01:04
  • @Ashley And `NUM_WEBPAGES` and `PAGE_LOGIN`? There is nothing intrinsically wrong with the code you have shown so far. If it doesn't do what you expect it to, it is because of some code you haven't shown yet. – walnut Oct 13 '19 at 01:08
  • @jdigital and uneven_mark Added a minimal reproducible example to the original post. It's not reproducible (yet)...sorry. We (on this side of the computer screen) are suspecting perhaps urls[] isn't populated by the time webpages[] is getting populated? – Ashley Oct 13 '19 at 01:16
  • @Ashley You ran into the static initialization order fiasco. I will look for a duplicate with good explanation, but google should tell you what that means as well. – walnut Oct 13 '19 at 01:18

0 Answers0