--My error - solved
Hi,
I am currently learning C++ from a book called Accelerated C++, whose website can be found here. I copied out the source code for producing a framed greeting message, and it is as follows:I then tried to compile it, but Borland said:Code://ask for a person's name, and generate a framed greeting #include <iostream> #include <string> int main() { std::cout << "Please enter your first name: "; std::string name; std::cin >> name; //build the message that we intend to write const std::string spaces(greeting.size(), ' '); const std::string second = "* " + spaces + " *"; //build the first and fifth lines of the output const std::string first(second.size(), '*'); //write it all std::cout << std::endl; std::cout << first << std::endl; std::cout << second << std::endl; std::cout << "* " << greeting << " *" << std::endl; std::cout << second << std::endl; std::cout << first << std::endl; return 0; }So, I thought there may be something wrong with my copied-out source code (I had checked the line mentioned against the line in the book and they were exactly the same). I downloaded the Borland sources for the programs in the book, and opened up their version of the program. It was exactly the same, and it also refused to compile, returning the same error. How can this be? The program MUST work, but my compiler denies this...Any help would be greatly appreciated, thanks in advance!"untitled1.cpp": E2451 Undefined symbol 'greeting' in function main() at line 12
edit: Just retried it and the Borland's source code does work...Here is Borland source:
I tried copying their "line 12" and putting it into my source code, but that didn't work...Code:// ask for a person's name, and generate a framed greeting #include <iostream> #include <string> int main() { std::cout << "Please enter your first name: "; std::string name; std::cin >> name; // build the message that we intend to write const std::string greeting = "Hello, " + name + "!"; // build the second and fourth lines of the output const std::string spaces(greeting.size(), ' '); const std::string second = "* " + spaces + " *"; // build the first and fifth lines of the output const std::string first(second.size(), '*'); // write it all std::cout << std::endl; std::cout << first << std::endl; std::cout << second << std::endl; std::cout << "* " << greeting << " *" << std::endl; std::cout << second << std::endl; std::cout << first << std::endl; return 0; }
edit2: My mistake! So sorry everyone, I must've missed the following lines:Sorry again!Code:// build the message that we intend to write const std::string greeting = "Hello, " + name + "!";
J_K9




Here is Borland source:
Reply With Quote