Hello,

im having this annoying problem with trying to create a global class to hold all the global variables to be used in the form. This is my main function:

// car rental.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
#include <string>
#include <stdlib.h>
#include <vcclr.h>

#include "Globals.h"

using namespace carrental;
using namespace System::IO;
using namespace std;


bool To_string( String^ source, string &target )
{    
	//Function to convert String^ to std::string
	pin_ptr<const wchar_t> wch = PtrToStringChars( source );    
	int len = (( source->Length+1) * 2);   
	char *ch = new char[ len ];
	bool result = wcstombs( ch, wch, len ) != -1;
	target = ch;   
	delete ch;  

	return result;
}

void ReadWord(StreamReader^ sr, string& dest)
{
	Char^ ch=(Char)sr->Read();
	String^ str;
	str=str+ch;
	for (int i=0; str[i]!=' ' && str[i]!='\n' && !sr->EndOfStream; i++)
	{
		ch=(Char)sr->Read();
		str=str+ch;
	}

	To_string(str, dest);
}

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	String^ path="C:\\Users\\Hani\\Documents\\input.txt";
	
	string temp;

	if (!File::Exists(path))
		return 0;
	
	StreamReader^ sr=gcnew StreamReader(path);

	for (int i=0; !sr->EndOfStream; i++)
	{
		ReadWord(sr, globals::test[i].type);
		ReadWord(sr, globals::test[i].brand);
		ReadWord(sr, globals::test[i].engine);
		ReadWord(sr, globals::test[i].tyres);
	}

	// Create the main window and run it
	Application::Run(gcnew Form1());


	return 0;
}

and this is my global header file:

#include <string>
using namespace std;

struct car
{
	std::string type;
	std::string brand;
	std::string manufacturer;
	std::string engine;
	int cylinders;
	std::string tyres;
};

public class globals
{
public: 
	static car test[100];
};

the errors im getting when i try to compile are:

1>car rental.obj : error LNK2020: unresolved token (0A000013) "public: static struct car * globals::test" (?test@globals@@$$Q2PAUcar@@A)

1>car rental.obj : error LNK2001: unresolved external symbol "public: static struct car * globals::test" (?test@globals@@$$Q2PAUcar@@A)

1>C:\Users\Hani\Desktop\car rental\Debug\car rental.exe : fatal error LNK1120: 2 unresolved externals

Dont bother yourselves with the details of the program, just tell what's wrong with what im trying to do...

Static members of a class still need to be defined outside of the class, and in a file that will not be included multiple times like a header:

// globals.h
#include <string>

struct car
{
	std::string type;
	std::string brand;
	std::string manufacturer;
	std::string engine;
	int cylinders;
	std::string tyres;
};

public class globals
{
public: 
	static car test[100];
};
// globals.cpp
#include "globals.h"

car globals::test[100];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.