473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Using late-binding with STL classes

Xx r3negade
I have a function, which reads input from a file and appends each line of the file to an STL object. I want to use this function with both lists and vectors. Since both of these objects have the push_back method, I thought I'd make a common function that can be used by both. Is there any practical way to do this?

Function show below:

Expand|Select|Wrap|Line Numbers
  1. template<class container>
  2. // The second argument here must be a list, vector, or any other STL object with a push_back() method
  3. void fileToContainer(const char* fName, container* vecptr) {
  4.     // Open a file and add each line to the list, vector, etc.
  5.     ifstream f(fName);
  6.     string line;
  7.  
  8.     if (f.is_open()) {
  9.         while (! f.eof()) {
  10.             getline(f, line);
  11.             if (line != "") {
  12.                 try {
  13.                     vecptr.push_back(line);
  14.                 }
  15.                 catch (...) {
  16.                     throw ios_base::failure("Invalid container object");
  17.                 }
  18.             }
  19.         }
  20.     }
  21.     else {
  22.         throw ios_base::failure("Could not open " + static_cast<string>(fName));
  23.     }
  24.  
Sep 25 '08 #1
5 1808
boxfish
469 Expert 256MB
I thought I'd make a common function that can be used by both. Is there any practical way to do this?
Is there something wrong with the function you have there? It seems like you have already done this.
Sep 25 '08 #2
I get this compile error

error: request for member ‘push_back’ in ‘vecptr’, which is of non-class type ‘std::vector'

Note that "vector" is the type passed to the function, in a different part of the code.

Edit: The function call looks like this:
Expand|Select|Wrap|Line Numbers
  1. fileToContainer<vector<string> >(argv[1], &ph.puzGrid);
  2.  
ph.puzGrid being a vector<string>, of course.
Sep 27 '08 #3
Any help please? Still not quite sure what to do...
Sep 30 '08 #4
boxfish
469 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. vecptr.push_back(line);
You can't use the push_back function on vecptr, because it is a pointer to a vector, not an actual vector. You have to dereference it first or use the arrow operator. Try:
Expand|Select|Wrap|Line Numbers
  1. vecptr->push_back(line);
Hope this helps.
Sep 30 '08 #5
*facepalm*
thanks :D
Oct 1 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: MarionEll | last post by:
--------------------------------------------------------- ************* Call for Participation ************** ************ Late Breaking News ************* *********** Extreme...
6
by: Sally Thompson | last post by:
My normal navigation menu is a <ul> and works fine. However, on one particular page I want to have a sub-menu when you go to that page, and I have been trying to achieve this with a nested <ul>. ...
14
by: Curtis Tammany | last post by:
Hello- Can someone tell me if DSOFile.dll can be accessed within ASP.NET? DSOFile.dll is registered and I have no problem using it in my .ASP scripts. I have tried the following: Dim...
1
by: Jeff B. Jr | last post by:
First off, let me say that I'm still relatively new, so please bear with me. My problem lies in creating a program that has two parts, a core application, and then other modules that can be added...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
1
by: Daniel | last post by:
How do I call a delegate in late bound C# dll? there some way to do this w/ a sharedinterface file? any examples? i tried this but it doesnt work: (oType.GetMethod("IOCTLJOB").Invoke(pObj, new...
32
by: whiteboy | last post by:
What's up Everyone? I really need some help. I've been pulling the hair out of my head trying to research how to calculate a late date. Here is what I'm trying to do: DATE VOUCHER...
1
snowman67
by: snowman67 | last post by:
I am writing an RFQ for a software to support my alternativecancer.us web site and need to add a late clause. I haven’t seen any, but I was thinking of a 1% penalty for the first week late and...
13
by: Wayne | last post by:
I have just experienced my first late paying client. By "late" I mean that the invoice is now almost 2 months overdue. I don't think that the client is maliciously withholding payment - rather...
4
by: James | last post by:
Hello everyone, While loading a page (http://www.edmonton.ca/portal/server.pt?space=CommunityPage&control=SetCommunity&CommunityID=239) into a webbrowser control I use invokemember on the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.