473,513 Members | 3,317 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Vectors in classes - public access

Hello,

I've got a VC++ project containing multiple classes and a main
function. In one of the class functions, it reads from a text file and
places the data into a vector;
//
std::vector<std::vector<std::string applications (50,
std::vector<std::string>(12));
applications[colArr][0] = "Test1";
applications[colArr][1] = "Test2";
//
Which works fine for that classes function, however, I need for other
functions of the class, or potentially other classes to be able to
access the applications vector, most likely done through friend-ing
and inheritance.
I seem to be unable to get the applications vector to be publicly or
even privately declared inside the main class definition. I've tried
placing the vector definition in both sections, it causes
error C2059: syntax error : 'constant'
when I do.
Is there something I'm missing with this?

Thanks,
- Andy
-- Copy of UpdateFileArray.h ---
#ifndef _UPDATEFILEARRAY_H_
#define _UPDATEFILEARRAY_H_

#include <string>
#include <vector>

class UpdateFileArray
{
std::vector<std::vector<std::string applications (50,
std::vector<std::string>(12));
public:
void UpdateArray();
void ArrayLookUp();
friend class SearchVehicle;
};
extern UpdateFileArray UFA;

#endif

May 11 '07 #1
5 2147
On May 11, 12:57 pm, andrewmor...@aol.com wrote:
Hello,

I've got a VC++ project containing multiple classes and a main
function. In one of the class functions, it reads from a text file and
places the data into a vector;
//
std::vector<std::vector<std::string applications (50,
std::vector<std::string>(12));
applications[colArr][0] = "Test1";
applications[colArr][1] = "Test2";
//
Which works fine for that classes function, however, I need for other
functions of the class, or potentially other classes to be able to
access the applications vector, most likely done through friend-ing
and inheritance.
I seem to be unable to get the applications vector to be publicly or
even privately declared inside the main class definition. I've tried
placing the vector definition in both sections, it causes
error C2059: syntax error : 'constant'
when I do.
Is there something I'm missing with this?

Thanks,
- Andy

-- Copy of UpdateFileArray.h ---
#ifndef _UPDATEFILEARRAY_H_
#define _UPDATEFILEARRAY_H_

#include <string>
#include <vector>

class UpdateFileArray
{
std::vector<std::vector<std::string applications (50,
std::vector<std::string>(12));
public:
void UpdateArray();
void ArrayLookUp();
friend class SearchVehicle;};

extern UpdateFileArray UFA;

#endif
Have a function in the vectors class to return a reference to the
vector.

One question though, why are you defining a vector of vector of
strings? Surely if you're just reading from a text file, a vector of
std::strings will be more than sufficent.

May 11 '07 #2
On May 11, 1:14 pm, Keith Halligan <keith.halli...@gmail.comwrote:
On May 11, 12:57 pm, andrewmor...@aol.com wrote:


Hello,
I've got a VC++ project containing multiple classes and a main
function. In one of the class functions, it reads from a text file and
places the data into a vector;
//
std::vector<std::vector<std::string applications (50,
std::vector<std::string>(12));
applications[colArr][0] = "Test1";
applications[colArr][1] = "Test2";
//
Which works fine for that classes function, however, I need for other
functions of the class, or potentially other classes to be able to
access the applications vector, most likely done through friend-ing
and inheritance.
I seem to be unable to get the applications vector to be publicly or
even privately declared inside the main class definition. I've tried
placing the vector definition in both sections, it causes
error C2059: syntax error : 'constant'
when I do.
Is there something I'm missing with this?
Thanks,
- Andy
-- Copy of UpdateFileArray.h ---
#ifndef _UPDATEFILEARRAY_H_
#define _UPDATEFILEARRAY_H_
#include <string>
#include <vector>
class UpdateFileArray
{
std::vector<std::vector<std::string applications (50,
std::vector<std::string>(12));
public:
void UpdateArray();
void ArrayLookUp();
friend class SearchVehicle;};
extern UpdateFileArray UFA;
#endif

Have a function in the vectors class to return a reference to the
vector.

One question though, why are you defining a vector of vector of
strings? Surely if you're just reading from a text file, a vector of
std::strings will be more than sufficent.- Hide quoted text -

- Show quoted text -
Hm, I'm a bit puzzled, what do you mean, return a reference to the
vector?

Also, vector of a vector of strings, since it needs to be two
dimentional. A line is read from the text file then split where spaces
occor, then the pieces are placed inside the vector.

May 11 '07 #3
On May 11, 8:45 am, andrewmor...@aol.com wrote:
On May 11, 1:14 pm, Keith Halligan <keith.halli...@gmail.comwrote:
On May 11, 12:57 pm, andrewmor...@aol.com wrote:
Hello,
I've got a VC++ project containing multiple classes and a main
function. In one of the class functions, it reads from a text file and
places the data into a vector;
//
std::vector<std::vector<std::string applications (50,
std::vector<std::string>(12));
applications[colArr][0] = "Test1";
applications[colArr][1] = "Test2";
//
Which works fine for that classes function, however, I need for other
functions of the class, or potentially other classes to be able to
access the applications vector, most likely done through friend-ing
and inheritance.
I seem to be unable to get the applications vector to be publicly or
even privately declared inside the main class definition. I've tried
placing the vector definition in both sections, it causes
error C2059: syntax error : 'constant'
when I do.
Is there something I'm missing with this?
Thanks,
- Andy
-- Copy of UpdateFileArray.h ---
#ifndef _UPDATEFILEARRAY_H_
#define _UPDATEFILEARRAY_H_
#include <string>
#include <vector>
class UpdateFileArray
{
std::vector<std::vector<std::string applications (50,
std::vector<std::string>(12));
public:
void UpdateArray();
void ArrayLookUp();
friend class SearchVehicle;};
extern UpdateFileArray UFA;
#endif
Have a function in the vectors class to return a reference to the
vector.
One question though, why are you defining a vector of vector of
strings? Surely if you're just reading from a text file, a vector of
std::strings will be more than sufficent.- Hide quoted text -
- Show quoted text -

Hm, I'm a bit puzzled, what do you mean, return a reference to the
vector?

Also, vector of a vector of strings, since it needs to be two
dimentional. A line is read from the text file then split where spaces
occor, then the pieces are placed inside the vector.

Look at your class, you are ignoring the constructor(s) altogether.
Declare a ctor and initialize the vector-vector in its initialization
list.

The following is a declaration:
std::vector<std::vector<std::string applications;
.... while this is a definition:
std::vector<std::vector<std::string
applications (50, std::vector<std::string>(12));
.... and definitions are not allowed in a class declaration.

#include <iostream>
#include <string>
#include <vector>

class UpdateFileArray
{
typedef std::vector<std::vector<std::string VecVecStr;
VecVecStr applications;
public:
UpdateFileArray()
: applications(10, std::vector<std::string>(10)) { }
// member functions
VecVecStr& getapp() { return applications; } // get ref to private v-
v
size_t size() const { return applications.size(); }
};

int main()
{
UpdateFileArray ufa;
std::cout << ufa.size() << std::endl;

std::vector<std::stringv(10, "some string");

typedef std::vector<std::vector<std::string VecVecStr;
VecVecStr& r_apps = ufa.getapp(); // seat the reference
r_apps.push_back(v);

std::cout << ufa.size() << std::endl;
}

Although i'm a little puzzled as well as to why you need a vector of
vectors as well.
If each line in the text file represents a Record composed of multiple
strings seperated by spaces, you should be storing std::vector<
Records where a Record class reflects the fields involved.

May 11 '07 #4
On 2007-05-11 14:45, an**********@aol.com wrote:
On May 11, 1:14 pm, Keith Halligan <keith.halli...@gmail.comwrote:
>On May 11, 12:57 pm, andrewmor...@aol.com wrote:


Hello,
I've got a VC++ project containing multiple classes and a main
function. In one of the class functions, it reads from a text file and
places the data into a vector;
//
std::vector<std::vector<std::string applications (50,
std::vector<std::string>(12));
applications[colArr][0] = "Test1";
applications[colArr][1] = "Test2";
//
Which works fine for that classes function, however, I need for other
functions of the class, or potentially other classes to be able to
access the applications vector, most likely done through friend-ing
and inheritance.
I seem to be unable to get the applications vector to be publicly or
even privately declared inside the main class definition. I've tried
placing the vector definition in both sections, it causes
error C2059: syntax error : 'constant'
when I do.
Is there something I'm missing with this?
Thanks,
- Andy
-- Copy of UpdateFileArray.h ---
#ifndef _UPDATEFILEARRAY_H_
#define _UPDATEFILEARRAY_H_
#include <string>
#include <vector>
class UpdateFileArray
{
std::vector<std::vector<std::string applications (50,
std::vector<std::string>(12));
public:
void UpdateArray();
void ArrayLookUp();
friend class SearchVehicle;};
extern UpdateFileArray UFA;
#endif

Have a function in the vectors class to return a reference to the
vector.

One question though, why are you defining a vector of vector of
strings? Surely if you're just reading from a text file, a vector of
std::strings will be more than sufficent.- Hide quoted text -

- Show quoted text -

Hm, I'm a bit puzzled, what do you mean, return a reference to the
vector?
Something like this:

class UpdateFileArray
{
typedef Apps std::vector<std::vector<std::string;
Apps applications (50, std::vector<std::string>(12));
public:
void UpdateArray();
void ArrayLookUp();
Apps& getArray() { return applications; }
const Apps& getArray() { return applications; }
friend class SearchVehicle;
};

--
Erik Wikström
May 11 '07 #5
On May 11, 1:07 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-05-11 14:45, andrewmor...@aol.com wrote:
On May 11, 1:14 pm, Keith Halligan <keith.halli...@gmail.comwrote:
On May 11, 12:57 pm, andrewmor...@aol.com wrote:
Hello,
I've got a VC++ project containing multiple classes and a main
function. In one of the class functions, it reads from a text file and
places the data into a vector;
//
std::vector<std::vector<std::string applications (50,
std::vector<std::string>(12));
applications[colArr][0] = "Test1";
applications[colArr][1] = "Test2";
//
Which works fine for that classes function, however, I need for other
functions of the class, or potentially other classes to be able to
access the applications vector, most likely done through friend-ing
and inheritance.
I seem to be unable to get the applications vector to be publicly or
even privately declared inside the main class definition. I've tried
placing the vector definition in both sections, it causes
error C2059: syntax error : 'constant'
when I do.
Is there something I'm missing with this?
Thanks,
- Andy
-- Copy of UpdateFileArray.h ---
#ifndef _UPDATEFILEARRAY_H_
#define _UPDATEFILEARRAY_H_
#include <string>
#include <vector>
class UpdateFileArray
{
std::vector<std::vector<std::string applications (50,
std::vector<std::string>(12));
public:
void UpdateArray();
void ArrayLookUp();
friend class SearchVehicle;};
extern UpdateFileArray UFA;
#endif
Have a function in the vectors class to return a reference to the
vector.
One question though, why are you defining a vector of vector of
strings? Surely if you're just reading from a text file, a vector of
std::strings will be more than sufficent.- Hide quoted text -
- Show quoted text -
Hm, I'm a bit puzzled, what do you mean, return a reference to the
vector?

Something like this:

class UpdateFileArray
{
typedef Apps std::vector<std::vector<std::string;
typedef [source] [target], you got it backwards
Apps applications (50, std::vector<std::string>(12));
This is C++, not Java, definitions go either in a ctor definition's
body, that ctor's init list or in a function definition.
Definitions go with definitions.
You aren't allowed to define members in a class declaration.
A declaration is for declarations.

class N
{
int n; // int n = 0; is illegal
N(); // also a declaration
void foo(); // a declaration
};

// a ctor *definition* with an init list
N::N() : n( 0 ) { }

// this is a definition:
void N::foo() { }
public:
void UpdateArray();
void ArrayLookUp();
Apps& getArray() { return applications; }
const Apps& getArray() { return applications; }
friend class SearchVehicle;

};

--
Erik Wikström

May 11 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4797
by: Billy Jacobs | last post by:
I have a website which has both secure and non-secure pages. I want to uses forms authentication. How do I accomplish this? Originally I had my web.config file in the root with Forms Authentication set up and it worked just fine. Then I realized that I needed to have some pages unsecure. I then created 2 directories. One named Secure and...
3
2031
by: Bryan Parkoff | last post by:
I have C++ Primer Third Edition -- Author Stanley B. Lippman and Josee Lajoie. I have been studying it for couple months however it does not provide a valuable information which it is about "friend to class". I am very disappointed because it is the way how C++ Compiler is designed. I assume that "friend to class" is not the good option....
10
17688
by: spoc | last post by:
I have been using memcpy to copy one class to another of the same type. There are reasons why I had to do this bug am getting some odd crashes and maybe I'm doing something dodgy copying classes like this? The classes contain no dynamicaly allocated data, just standard types and arrays. Here is an example of what I'm doing, I know in isolation...
0
3911
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header,...
11
1895
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The destruction of the assigned memory seems to call the class destructor more than once. I don't know the reason or whether I used the vector class...
2
1649
by: sampandaran | last post by:
Hey, I'm having some trouble with a program I'm trying to write and was hoping someone here could help me. Basically I have a dynamic 2D array of the 'Square' class and in the square class is the pointer 'item.' 'item' is a pointer of another class that I have called 'Inhabitant' and I have 3 derived classes off of 'Inhabitant.' I need to somehow...
2
1578
by: Ben Wheare | last post by:
Hiya, I'm fairly new to C++, and trying to figure out how to do the following: I have a class, strings, which has a private char array. To add to this array, I call the member function add_array(int), with an int, which populates the array (in this case with the binary equivalent of that integer). I want to make another class,...
7
2145
by: Rob | last post by:
This actually compiles and works but it doesn't seem like the best code, so I was wondering is there another way to do this? template <typename Tvector<T>* addDepth(T) { return new vector<T>; } ....a templated meth...
7
2798
by: =?Utf-8?B?QU9UWCBTYW4gQW50b25pbw==?= | last post by:
Hi, I have been using the code (some of it has been removed for simplicity) below to allow authenticated (using ASP.NET membership database) users to get a file from their archive area. It seems to work fine, however I noticed that no web log entry is added when a successful download occurs (normally a 200 HTTP status code, however, if...
0
7269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.