1

i am doing my first c++/CLI project with windows form. The main idea is to make a graphical interface that uses data that i get from a server. I use curl to talk to server which gives me a json string. I then use rapidJson. I am new to c++ so please be patient with noobie mistakes. My file called Myform.h includes skapakonto.h. I am getting the following:

Error C2039 'UTF': is not a member of 'Project1' line 149

code where i am getting error is in file called skapaKonto.h

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^  e) {
    int httpCode(0);
    std::string readBuffer;
    //string* retval;
    if (curl) {
        String^ anamn = this->textBox1->Text;
        const char* url = "serverip/TP/Admin/funktioner/skapa.php";
        string param = "nyckel=iRxOUsizwhoXddb4&funktion=skapaAKonto&anamn=" + msclr::interop::marshal_as<std::string>(anamn) + "&tjanst=47&rollid=6";
        //string param = "nyckel=iRxOUsizwhoXddb4&funktion=skapaAKonto&anamn=BJORN&tjanst=47&rollid=6";
        const char *data = param.c_str();
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Project1::UTF::callback); //line 149, where error originates
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);

        ret = curl_easy_perform(curl);
        if (ret != CURLE_OK)
            Console::Write("fel på begäran");

        cout << readBuffer;
        curl_easy_cleanup(curl);

        const char* json = readBuffer.c_str();

        Document d;
        d.Parse(json);

        StringBuffer buffer;
        Writer<StringBuffer, Document::EncodingType, UTF8<> > writer(buffer);
        d.Accept(writer);
        const char* output = buffer.GetString();
        std::cout << output;

        MessageBoxA(NULL, output, "serversvar:", MB_OK | MB_ICONQUESTION);
    }
}
};

i have 2 namespace Project1, 1 in each file. MyForm.h has the namespace UTF in it. If i put the content of namespace UTF in skapakonto.h i get errors related to defining something multiple times (even if i name it something else). This is the top of skapaKonto.h where i have inclusions:

#pragma once
#include <ctime>
#include <list>
#include <string>
#include <cmath>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
#include <msclr\marshal_cppstd.h>
#include "rapidjson/encodings.h"
#define CURL_STATICLIB
#include <curl/curl.h>
#include <algorithm>
#include <codecvt>

namespace Project1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace rapidjson;
using namespace std;

I am thinking this might be an error to do with inclusions or namespace. Any thoughts on what i am doing wrong/potential solutions?

walnut
  • 21,629
  • 4
  • 23
  • 59
pzyqux
  • 64
  • 8
  • 1
    Sooo many using namespace. That might be the cause of the problem. You should take a look at [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Lukas-T Dec 16 '19 at 14:20
  • @churill thank you, i will look into this. – pzyqux Dec 16 '19 at 14:21
  • Concerning your actual problem: if I understnad you correctly and you have the implementation of `button1_Click` in the same file as the namespace `UTF` be sure that they are in correct order. – Lukas-T Dec 16 '19 at 14:23
  • Move all your `using`s out of the `Project1` namespace. Don't litter your own namespace with system names. – molbdnilo Dec 16 '19 at 14:23
  • Should not be tagged `c++`. C++/CLI is not C++. Much as Objective-C++ is not C++. Less similar but still in the same vein, as JavaScript is not Java. – Eljay Dec 16 '19 at 14:23
  • @eljay sorry about that. Can i remove a tag? – pzyqux Dec 16 '19 at 14:25
  • @molbdnilo when i do that there is no difference sadly. – pzyqux Dec 16 '19 at 14:26
  • @pzyqux You should do it anyway. Including other namespaces wholesale inside your own namespace goes against the idea of namespaces. – molbdnilo Dec 16 '19 at 14:27
  • 1
    C++/CLI is *not* C++. It's a separate language designed for the sole purpose of building .NET native interop layers. C++/CLI is not meant to build Windows Forms applications in. Microsoft themselves recommend to not use C++/CLI to create Windows Forms applications. The C++/CLI Windows Forms project template was removed for this very reason…because it made people think that was a way to build Windows Forms applications using C++. It's not. You cannot build Windows Forms applications using C++. Windows Forms are part of the .NET framework, which is a managed code library not accessible from C++… – Michael Kenzel Dec 16 '19 at 14:29
  • 1
    Do yourself a favor and don't use C++/CLI unless you have to. If you want to build Windows Forms applications use C# or one of its siblings. There is nothing to gain here from using C++/CLI. The fact that you're using C++/CLI does not make the Windows Forms code any less managed than it is… – Michael Kenzel Dec 16 '19 at 14:39
  • 1
    @pzyqux I removed the `c++` tag for you, but you can also edit tags yourself. Just click [edit] and modify them below the question body. – walnut Dec 16 '19 at 14:47
  • @walnut thank you! – pzyqux Dec 17 '19 at 07:31
  • @MichaelKenzel in that project i had to use c++/cli, i have learned now not to do anything like that again. – pzyqux Feb 18 '20 at 13:03

1 Answers1

0

What i had to do in the end was remove projekt1:: before UTF::Callback, and put inline infront of callback where i create the function. That solved the issue

pzyqux
  • 64
  • 8