-3

I try to add two QStrings into one. I've read a lot about:

QString NAME = QString + QString

but this doesn't helps me here. Thats how my code look so far:

test.h

#ifndef TEST_H
#define TEST_H

#include <QString>
#include <QFile>
#include <QDir>

class Test
{
public:
    void createProject(QString* p, QString*n);
};

#endif // TEST_H

test.cpp

#include "test.h"
#include <QFile>
#include <QString>
#include <QDir>

void Test::createProject(QString *p, QString *n)
{
    QString result = p + n;
    QDir dir(result);
    if (dir.exists())
    {
        // ok
    }
    else
    {
        printf("Error!\n");
    }
}

(ignore the code about checking if the directory exists, btw I use Qt 4.8.6)

So now when I try to compile, I get this error:

test.cpp: In member function 'void Test::createProject(QString*, QString*)': test.cpp:8:21: error: invalid operands of types 'QString*' and 'QString*' to binary 'operator+'
QString result = p + n;

How can I make this work? Also using += instead of + doesn't works here.

~ Jan

Jan
  • 111
  • 1
  • 2
  • 4
  • 1
    You are trying to find sum of two pointers which is not allowed. What you probably want to do is to pass `QString` as references or values. – Predelnik May 16 '15 at 08:33

1 Answers1

4

Indeed you are adding their address as p and n are pointer. try adding their value as:

QString result = *p + *n;
Emadpres
  • 3,466
  • 2
  • 29
  • 44
  • Thanks! This worked. However I read too much about QString = QString + QString but not remembered about pointers ;) – Jan May 16 '15 at 08:41