In a c++ tutorial this was one way to define a string
#include <string>
using namespace std;
string name;
name="mark"; But it dosent work i get errors.Why is this?
The original compile failures come from two separate things: trying to use an assignment statement at namespace (global) scope, and a few small syntax typos in the class code. correctly asked about a main() function, was right to stress the difference between initialization and assignment, and spotted the missing parentheses on the destructor. Below is a short clarification and a safe, idiomatic pattern.
At namespace scope a definition may perform initialization, but standalone assignment statements are not allowed. For example, this is valid at global scope:
std::string globalName = "mark"; // initialization at declaration — OK This is not valid outside any function:
globalName = "mark"; // illegal at namespace scope — must be inside a function For class members prefer a constructor initializer list and make sure function signatures are correct (no stray semicolon after int main(), destructor must be ~Name() including ()):
#include <iostream>
#include <string>
class Name {
std::string myname;
public:
Name();
~Name();
void print() const;
};
Name::Name() : myname("mark") { }
Name::~Name() { }
void Name::print() const { std::cout << myname << '\n'; }
std::string globalName = "mark";
int main() {
std::cout << "global: " << globalName << '\n';
Name firstname;
firstname.print();
return 0;
} Quick troubleshooting notes: common compiler diagnostics will point to the first syntax problem — a stray semicolon after int main() or a missing () on ~Name typically produces confusing follow-up errors. Using -Wall -Wextra (or enabling warnings in the IDE) makes these issues obvious. Also prefer explicit std:: qualifiers in small examples to avoid ambiguity while learning.
Jump to Post— Eddy Dean 13Don't you think you need a Main() function?
Jump to Post— Eddy Dean 13#include <iostream> #include <string> int main() { string name; name="mark"; cout<<name; return 0; }
Don't you think you need a Main() function?
Don't you think you need a Main() function?
yes a got a main function
#include <iostream>
#include <string>
string name;
name="mark";
int main()
{
cout<<name;
return 0;
} #include <iostream>
#include <string>
int main()
{
string name;
name="mark";
cout<<name;
return 0;
} or maybe:
#include <iostream>
#include <string>
string name("mark"); //this is initialization, not assignment
int main()
{
cout<<name;
return 0;
} However, I don't think you can assign values to global variables outside of a function.
Thx that worked but now i want to put a string in a class i tryed this
#include <iostream>
#include <string>
class Name
{
string myname;
public:
Name();
~Name();
void print();
};
Name::Name()
{
myname="mark";
}
Name::~Name
{
}
void Name::print()
{
cout<<myname;
}
int main();
{
Name firstname;
firstname.print();
return 0;
} Didnt work
never mind got it working:)
Thx that worked but now i want to put a string in a class i tryed this Didnt work
I have highlighted the section which you forgot towrite or had wrongly written.
Try this
#include <iostream>
#include <string>
class Name
{
string myname;
public:
Name();
~Name();
void print();
};
Name::Name()
{
myname="mark";
}
Name::~Name ()
{
}
void Name::print()
{
cout<<myname;
}
int main();
{
Name firstname;
firstname.print();
return 0;
} It would be really better if you started C++ using some basic c++ tutorials, get the basics straight, read some theory and then start programming.
Hope it helped,
bye.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.