0

I've recently been relearning C++ and creating an application in hand with what I'm learning and even branching out to figure out concepts that aren't necessarily normal. As dangerous as I see this being, I am still diving head first.

That being said the application I'm currently working on requires storing some info in a local database which I will then be merging with a server database at some point in the future to allow for more in-depth queries and a better UI. While diving into learning SQLite3 integration with C++ I've found that a lot of the integration is specifically "C/C++" with, what appears to be, a stronger foot in C than C++. With this realization I've come across one very specific tutorial that leaned on C++ over C minus the specific issue I'm encountering.

https://www.dreamincode.net/forums/topic/122300-sqlite-in-c/

I actually rather like the concise nature of the Database.cpp that the author of the tutorial created and I want to utilize it. The problem is C++ likes to throw the conversion warnings that apparently work due to the use of C, but are deprecated in C++.

ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

Technically this can be bypassed by casting the string to (char*). While I understand this it seems I may be missing some information as well. Could this be bypassed by changing the parameters of the Database function to "string*" then converting it to "char*" in the function or should I not care about the implicit conversion and just ignore it. I really don't want to cross C and C++ in my application so I would prefer to paid heed to the warning. Figured I'd ask for advice to at least get some clarification though.

If it seems obvious from my inquiry that I am lacking in some very specific section of my C++ knowledge please feel free to let me know. I am nothing if not diligent when it comes to making sure I can fill all the gaps in my knowledge on any given topic.

7
  • See std::string::c_str() method in your favorite C++ Reference. Commented Jul 11, 2018 at 1:34
  • If I follow your question the issue is you have a function void foo(char* name) which is called like foo("Name"); but "Name" is a const char* not char*. So, just add const in the function declaration (e.g., void foo(const char* name)). Commented Jul 11, 2018 at 1:35
  • 1
    These problems are entirely with the tutorial's somewhat sloppy code; you shouldn't try to stuff string literals into non-const pointers in C either. My experiences with sqlite have found it to be const correct, so you should be able to follow @JamesAdkison 's advice and merely make the tutorial code const-correct and happy in modern C++. In general, don't cast unless you can't fix whatever is making you cast. Commented Jul 11, 2018 at 1:56
  • Can you please edit your post to include the exact line that is generating that error please? Commented Jul 11, 2018 at 2:34
  • It's all sorted guys, see below. Commented Jul 11, 2018 at 3:47

1 Answer 1

1

You should tell the author of that tutorial to make his code const correct if you want to make use of his class.

Well, actually, I suggest you edit it yourself to make it conform. This won't take you long once you understand the principles involved (which are not hard to get your head around), and doing this will help you write your own code in the right way.

So, just by way of example, change this:

class Database
{
public:
    Database(char* filename);
    ...

To this (note the added const):

class Database
{
public:
    Database(const char* filename);
    ...

There are, of course, a bunch of other related changes you need to make but you just have to see it through, and the compiler itself will guide you when you get it wrong. SQLlite itself is already const correct (because those guys are professionals), so there is light at the end of the tunnel.

Sign up to request clarification or add additional context in comments.

Comments

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.