473,395 Members | 1,742 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,395 software developers and data experts.

Problem with generic function

Hello,

I am new to c++. I tried to create a function for doing the following:

1. Read a matrix from a .txt file
2. Store the retrieved data as a vector of vectors
3. Put the result in screen.

I could somehow complete parts 1 and 2. However, I am having lots of problems with the third part. I tried to use a generic function, and I am getting this complaint from my compiler:

d:\dogma\data\research\codes\zcppzone\test01\sscr. h|9|error C2065: 'T' : undeclared identifier

T there, is the type of the generic function. It is supposed to be ambiguous.
That is the problem. Any idea is appreciated. I am learnign this by myself so any kind of modification is welcome.

My codes:

Main function:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "sscr.h"

using namespace std;

int main(){

// Variable declaration and initialization

string x;
typedef string::size_type string_size;
string_size i = 0, j, sz;

vector <vector<int> > adjm;
vector<int> data;

ifstream adj("adj.txt");
istringstream sxp;

int hi;


// Actions

getline(adj, x);
sxp.str(x);
sz = x.size() - 1;

while (i != sz){

for (j = 0; j != sz; ++j){
sxp >> hi;
data.push_back(hi);
}
adjm.push_back(data);
sxp.clear();
data.clear();

getline(adj, x);
sxp.str(x);

++i;

}

// Closing commands
cout << "the number of items in a row of the file is: " << sz << endl;

int p = sscr(adjm);

return p;

}


Generic function:

#include <iostream>
#include <vector>
#include "sscr.h"

using std::vector; using std::iostream;

template <class T>
int sscr(vector<T> v){

for (vector<T>::const_iterator iter = v.begin(); iter != v.end(); ++iter){

for (vector<T>::const_iterator jter = (*iter).begin(); jter != (*iter).end(); ++jter){
if (jter != (*iter).end() - 1)
cout << *jter << " ";
else
cout << *jter << endl;
}
}
return 0;
}

Generic function header:

#ifndef SSCR_H_INCLUDED
#define SSCR_H_INCLUDED

#include <iostream>
#include <vector>

using std::vector; using std::iostream;

int sscr(std::vector<T>);

#endif // SSCR_H_INCLUDED

Thanks in advance.
Aug 14 '08 #1
1 1691
Banfa
9,065 Expert Mod 8TB
I am guessing that the code for your generic function sscr is in a CPP source file and that you are trying to compile it.

That is not how you use template functions (or classes). You should put the code into the header and just include the header into the source file you are calling the function from.

This is because a template function is not a definition of a function (that would normally go in a source file) but rather the declaration of the pattern for the function. The complier needs to see that pattern where it being called from and then from the pattern and the call it can work out the definition of the function that it automatically adds to the program for you.
Aug 14 '08 #2

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

Similar topics

45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
11
by: ajay.sonawane | last post by:
Hello ther I read somewhere that callback function should be global or static member function. 1: I could use static member functions but how could I access members of class which ar not static....
6
by: nizar.jouini | last post by:
I have web page that contains two links. link "a" and link "b". When I click on "a" a small window should pop up. when I click on "b" another small window should pop up. So what you should see now...
1
by: Steemer | last post by:
Okay, I checked with usual suspects and I'm still frustrated. ..txt file, fixed width. About 88 fields of varying length. All text fields. It will only let me add seperators to about the 410th...
6
by: Urs Eichmann | last post by:
While experimenting with the Feb CTP Edition of VB 2005, I came across "generic procedures". You can write: Public Class Foo Public Sub MySub(Of tDisp As IDisposable)(ByVal vMyParm As Integer)...
3
by: markww | last post by:
Hi, I have a wrapper around some 3rd party database library function. The pseudo code looks like the following - it is meant to open a table in a database, extract values from a table, then copy...
16
by: shapper | last post by:
Hello, I have a generic list as follows: Dim rows As New Generic.List(Of row) Now I have a row: Dim myRow As row I tried to check, further in my code, if the row is nothing: If myRow Is...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
10
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...

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.