Hi there,

At university I learnt Fortran to an adequate level for simple programs and as I enjoyed it i looked towards trying to learn C++. Up to now its been very slow but progressing, however i seem to have hit a bit of a brick wall.

From just material on the internet I have found this one of the best and most friendly websites so was hoping to hopefully get a bit of help without looking too stupid lol.

Without going off on one im basically trying to define a string that is part of a class and then bring it into the main function for output. This may seem strange but I think it is a requirement for my program which at the end will span a fair few source files.

Anyway this is my code at present:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
 
class test 
{
public:
	void write(std::string);
};
 
void test::write(std::string name)
{
	name = "HELLO";
}


int main ()
{
	  test A;
	  cout << A.write(name) << endl;
	  return 0;
}

I am revcieving an error:

error C2065: 'name' : undeclared identifier

Do I require the use of pointers perhaps?

I hope this is enough for someone to understand.

Thank you very much in advance for your time in looking at this,

Andy

Try this and see if it works for ye'...

std::string& write();

std::string& test::write()
{	
          std::string *name = "HELLO";
          return name:
}

Hi mate,

Thanks for your reply. I have implemented the code as shown below but for some reason it still comes up with an error or two although they do look a lot more promising though lol:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
 
class test 
{
public:
	std::string& write();
};
 
std::string& test::write()
{
	std::string *name = "HELLO";
	return name;
}


int main ()
{
	  test A;
	  cout << A.write() << endl;
	  return 0;
}

errors:
1)error C2440: 'initializing' : cannot convert from 'const char [6]' to 'std::string *'
2)error C2440: 'return' : cannot convert from 'std::string *' to 'std::string &'

Sorry if im being a numpty and maybe misinterpreted what you where saying!

In your first post you haven't defined a name within scope. I think what
you are trying to is something like this :

#include<iostream>
#include<string>
using namespace std;

class Test
{
    string name;
    public :
     Test(string str) { name = str; }
     Test() { name = "NULL"; }
     void setName(string str) { name = str; }
     string getName() { return name; }   
};

int main()
{
    Test test1("Greatest 4 eva");
    cout << test1.getName();
    test1.setName("4 eva the greatest");
    cout << test1.getName();
  return 0;
}

You can do this

class test 
{
public:
    std::string write();
};
 
std::string test::write()
{
	std::string name = "HELLO";
	return name;
}


int main ()
{
	  test A;
	  cout << A.write() << endl;
	  return 0;
}

Hi,

Thanks for the above, it's useful for me however I dont think I have managed to explain myself correctly so sorry for that.

Essentially what I am trying to do is crete a string that wont ever change in a class that can later be passed onto the main function. I know this may sound odd but at the end I would like to be passing a number of constant strings to the main.

eg.

class
{
//declaration of string
}

string
{
//definition of the string eg hello
}

main
{
//string passed into main
}

The way my program is currently written wont really accept a diferent strategy without a major overhaul which im not too keen to do considering my control of the c++ language.

I hope this clears my intentions up and sorry if ive been misleading.

Thanks again,

Andy

Hi again,

I think I posted just after you posted Ancient Dragon.

Your code above has worked perfectly.

Thank you for all your quick replies, is very much appreciated.

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.