QString::toWCharArray error LNK2019

Hi!
I'm fairly new to programming in C++ (only done Java before). I try to read data from a *.dat file.
As this file is selcted by the user via a GUI(done with QT) there isn't one special file to test with.

I wrote the following method to read the data - there are 3 times 2 bytes at the beginning of each *.dat file which specify the amount of data given for x, y and z direction
- this is why I only try to read these 2 bytes for the beginning

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void MainWindow::loadFileData(const QString &fileName){
ifstream is;
WCHAR * file;
fileName.toWCharArray(file); 
is.open (file, ios::binary );
	

int length=2;
char * sizeX = new char [length];
char * sizeY = new char [length];
char * sizeZ = new char [length];
	
is.read (sizeX,length);
is.read (sizeY,length);
is.read (sizeZ,length);
}


I did include:
#include <QtGui>
#include <iostream>
#include <fstream>
#include <QString>
#include <string>

#include "mainwindow.h"
#include "glwidgetmain.h"
#include "window.h"
#include "stdio.h"

when trying to build the project I receive the following error:

1>------ Build started: Project: Visualisierung, Configuration: Debug Win32 ------
1>Compiling...
1>mainwindow.cpp
1>c:\vislu\vis_proj\mainwindow.cpp(194) : warning C4700: uninitialized local variable 'file' used
1>Linking...
1>mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: int __thiscall QString::toWCharArray(wchar_t *)const " (__imp_?toWCharArray@QString@@QBEHPA_W@Z) referenced in function "private: void __thiscall MainWindow::loadFileData(class QString const &)" (?loadFileData@MainWindow@@AAEXABVQString@@@Z)
1>C:\vislu\vis_proj\Debug\Visualisierung.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\vislu\vis_proj\Debug\BuildLog.htm"
1>Visualisierung - 2 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I guess I missed an include but I can't get rid of this error so far no matter what I try.

Thanks for your help!
Last edited on
Have you linked against the appropriate QT library?.
yes I linked against "$(QTDIR)\lib"; where QTDIR is defined in the windows environment variables as path to the QT folder
all other methods needing QT do work fine - so I guess linking QT is ok
It is just that:
1. The QString stuff is in the qtcore library (QT has about 30 libraries)
2. The QString::toWCharArray function did not appear in QT until version 4.2


(And another thing is that you actually have to allocate the WCHAR buffer - which you are not actually doing);
Thanks for your help so far!

I use QT 4.5.3 - do I have to link each library needed on its own? I do link the whole library folder which holds QtCore4.lib as well. Is this the one meant?

concerning the WCHAR issue I added the following:

int namelength = fileName.length();
WCHAR * file;
file = (WCHAR *) malloc (namelength);
fileName.toWCharArray(file);

to allocate the buffer - I'm not sure wether I did it correctly?
Unfortunately I still receive the same error!
You link libraries as required.
QtCore4.lib is the one I'm particularly thinking about (QtCore4d.lib is the debugging version)
How are you using qt - are you using an IDE - if so which one - we should be able to get this thing sorted in no time at all.
I'm not using an IDE - I downloaded the QT sources, compiled them and installed the addin for msvc
I started using the QT creator but switched to doing the GUI moddeling without creator
Ah, that is what I use. I really like it.
Bring up the project properties dialog and go linker ->input
What (libraries) have you got listed in the Additional Dependencies box??
yeah QT works quite nice - but it's a big change to switch from Java and especially Eclipse to C++ and MSVC

this is what I've got as Additional Dependencies:

opengl32.lib glu32.lib gdi32.lib user32.lib $(QTDIR)\lib\qtmaind.lib $(QTDIR)\lib\QtOpenGLd4.lib $(QTDIR)\lib\QtGuid4.lib $(QTDIR)\lib\QtCored4.lib glew32.lib devil.lib

Damn, looks like the appropriate library is being linked.
I will try to duplicate the error tomorrow.
I tried the function and it worked for me.

(My QT version is 4.3.3).


(Do a project clean and rebuild and see what happens)
Goto the project settings in VS and uncheck the Treat wchar_t as a built-in type to No in the C/C++ compiler language option.
Topic archived. No new replies allowed.