Problem with "string" in a "class".

Problem with "string" in a "class".

Post by J. Z » Mon, 24 Jul 2000 04:00:00



Hi, there:
Thanks for prompt reply for m former questions.  Now, it is no problem with
the "string" array declaration.  The new problem is encountered in a "class"
function whose return value is a "string".  Here is the main and the head
file.

Head File:
----------------------------------------------------
#include <stdlib.h>
#include <string>
#include <iomanip>
#include <cctype>

class StringArray
{

 std::string table[2];
public:
 StringArray();
 std::string get(int location);

Quote:};

StringArray::StringArray()
{
table[0]="abcd";
 table[1]="efgh";

Quote:}

std::string get(int location)
{
 return table[location];     /** Error found here**/

Quote:}

#endif
--------------------------------------

Main Program:
-------------------------------------
file://File: Main.cpp
#include <iostream>
#include <stdlib.h>
#include <string>
#include <iomanip>
#include <cctype>
#include "StringArray.h"

using namespace std;

StringArray Array;

int main()
{
 cout<<Array.get(0)<<", "<<Array.get(1)<<endl;

 return 1;

Quote:}

-----------------------------------------

Error Message:
---------------------------------------
--------------------Configuration: Main - Win32 Debug--------------------
Compiling...
Main.cpp
d:\classes\data structure\p4\string\stringarray.h(29) : error C2065: 'table'
: undeclared identifier
d:\classes\data structure\p4\string\stringarray.h(29) : error C2109:
subscript requires array or pointer type
d:\classes\data structure\p4\string\stringarray.h(29) : error C2664:
'__thiscall std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >::std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >(con
st class std::allocator<char> &)' : cannot convert parameter 1 from 'int' to
'const class std::allocator<char> &'
        Reason: cannot convert from 'int' to 'const class
std::allocator<char>'
        No constructor could take the source type, or constructor overload
resolution was ambiguous
Error executing cl.exe.

Main.obj - 3 error(s), 0 warning(s)
------------------------------------------

 
 
 

Problem with "string" in a "class".

Post by Victor Bazaro » Mon, 24 Jul 2000 04:00:00



Quote:> Hi, there:
> Thanks for prompt reply for m former questions.  Now, it is no problem
with
> the "string" array declaration.  The new problem is encountered in a
"class"
> function whose return value is a "string".  Here is the main and the head
> file.

> Head File:
> ----------------------------------------------------
> #include <stdlib.h>
> #include <string>
> #include <iomanip>
> #include <cctype>

> class StringArray
> {

>  std::string table[2];
> public:
>  StringArray();
>  std::string get(int location);
> };

> StringArray::StringArray()

inline StringArray::StringArray()

Quote:> {
> table[0]="abcd";
>  table[1]="efgh";
> }

> std::string get(int location)

inline std::string StringArray::get(int location)

Quote:> {
>  return table[location];     /** Error found here**/
> }

And it is better NOT to put implementation in a header file
unless it's a template impmementation.

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

1. "own" vs "uses" vs "contains" vs "is a"

I am studing for a VB exam and the book I have mentions there are
relation types between objects. (New Riders pg. 22 "MCSD Visual Basic
6 Exams 70-175 and 70-176".  (FYI - I did take a course in Object
Oriented Design!)

The book does not give any examples, but I have come up with the
following examples for the relation types that the book says exist. I
came up with the examples etc. from another book by Kurata ( Doing
objects in Visual Basic 6".

My question is how is "contains" different from "has a" I cannot come
up with an example.

"is a" - example a programer "is a" employee. In VB this might be
called a subclass.

"uses" - a data screen "uses a" employee object. In VB this might be
called a collaborator.

"has a" - and employee "has a" time sheet. In VB this might be called
a container.

"contains" -

2. Priority Queuing With traffic shaping

3. const char *p = "string" or const char p[] = "string"?

4. Benchmarks for VC++ on an Alpha

5. Removing "My Computer", "Recycle Bin" and "Network Neighborhood" from desktop

6. WTB FDDI for IO4

7. Why "public" "private" "protected"

8. Guest account

9. extern "FORTRAN", extern "Pascal", extern "Ada" ... ?

10. "remove" is not a memeber of "string" in the command line compiler?

11. can I use "class" instead of "struct" in win98 DDK?

12. O/S headers define "struct map" conflicts with STL "class map"

13. "distributed" class "pattern"