Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8587159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:30:11+00:00 2026-06-11T22:30:11+00:00

I’m somehow new to templates and I’m trying to modify some library that provide

  • 0

I’m somehow new to templates and I’m trying to modify some library that provide matrix and vector operation to c++, i have a vector class which I’m trying to overload the operator() for it to handle an operation like this Vector(2:5) will return a vector that has elements 2,3,4,5 of the original vector, and I’m using a class called colon where colon(2:5) will represent the (2:5) effect as i found that c++ has no operator: .
hope i gave a proper introduction.
The relevant code is as follows

The Vector Class

template< size_t M, typename T = float >
class Vector
{
public:
typedef T   value_type;
inline T& operator()( size_t index );
inline const T& operator()( size_t index ) const;
template <size_t N> Vector<N,T> operator()(const  colon &cex) const;
.
.
}

and the corresponding implementation

template< size_t M, typename T >
template< size_t N>
Vector<N,T>
Vector<M,T>::operator()( const colon &cex ) const
{
long i, ii, st = 0, in = 0, en = 0, s;
cex.apply(M, st, in, en, s);
if (s && (st>0) && (st>M))
{
Vector<N,T> result;
for (i=st,ii=0;i+=in,ii++;i<=en,ii<N)
{
result(ii)=array(i);
return result;
}

}
return 0;
}

the return 0 here is just a place holder, it should return an empty vector.
the colon class (which is taken from another library and modified by me).

class colon
{
public:
/// Colon expression '(:)'
colon() { _flag = 'a'; }
/// Colon expression of type '(2:5)'
colon(long s, long e) { _s = s; _i = 1; _e = e; _flag = 'r'; }
void apply(long s, long &f, long &i, long &l, long &n) const;

private:
/// Type of colon expression.
char _flag;
/// First index.
long _s;
/// Increment.
long _i;
/// Last index.
long _e;

}; /* class colon */

and the relevant implementation is

void
colon::apply(long n, long &st, long &in, long &en,
     long &le) const
{
switch (_flag)
{
    case 'r':
        if ((_i == 0 ) || ((_e - _s) / _i < 0 )) le = 0;
        else
        {
            st = _s;
            in = _i;
            en = _e - (_e - _s) % _i;
            le = (_e - _s) / _i + 1;
        }
        break;
    case 'a':
        if (n)
        {
            st = 1;
            in = 1;
            en = n;
            le = n;
        }
        else le = 0;
        break;

 }
}

The code used to test the functionality is

bool ok = true;

Vector< 4, double > v;
double data[] = { 1, 2, 3, 4 };

v.iter_set( data, data+4 );//just puts elements of data inside v with the same type

// test  Vector colon
{
    bool ok = true;
    Vector<3,long> test;
    test=v(colon(2,4));//Problem

}

The error now is

C2664: 'const double &Vector<M,T>::operator ()(size_t) const' : 
cannot convert parameter 1 from 'colon' to 'size_t'

output of compiler is

error C2664: 'const double &Vector<M,T>::operator ()(size_t) const' : 
cannot convert parameter 1 from 'colon' to 'size_t'
with
[
M=4,
T=double
]
No user-defined-conversion operator available that can perform this conversion, 
or the operator cannot be called

???? help is appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:30:12+00:00Added an answer on June 11, 2026 at 10:30 pm

    I have too much to say to fit it into a comment, so I will use the answer for this (although it does not answer your question directly):

    You need to know what your use cases will be.
    If you are going to do much with runtime variables, then there is no sense in implementing it all in templates. If you want to use templates only, then you need to do so too with class colon. I will give you some hints to both.

    First let me explain your problem in more detail than what the comments could say.

    Lets see few use cases assuming we have a Vector<5, int> vec;.

    Case “compile-time size”:

    //get the entries with indices 1 to 3 (including)
    Vector<3, int> vec2 = vec(colon(1, 3));
    

    For this code everything is known at compile time so it would be possible to do this at compile time (though it is unnecessary ugly for this purpose).

    Case “run-time size”:

    //we get the variables a and b as input from the user at runtime
    Vector<?, int> vec2 = vec(colon(a, b));
    

    See those a and b variables? Do you know what values they hold, when you write the program? No you cannot because the user should input them. They could be 0:0 or 0:4 or 42:1337, who knows what the user will enter (accidentally). When you do not know the value of a and b you also do not know what will stand at ?, but you will have to know at compile-time, because every template argument has to be known at compile-time.

    Now you need to know what you want to do. Do you want to work with a:b (run-time parameters) or do you only ever want to enter constants like 1:3 (compile-time parameters). Depending on this you need to choose, how to implement Vector<n,T>::operator().

    You see: As soon as the size of the Vector depends on run-time values it cannot be determined at compile-time and it is therefore not possible to return the right sized vector.

    You could however implement a compile-time-sized and a run-time-sized vector and colon class to enable both at the cost of a more complex implementation. They would give following pattern:

    CompileTimeVector CompileTimeVector::operator()(CompileTimeColon);
    RunTimeVector CompileTimeVector::operator()(RunTimeColon);
    RunTimeVector RunTimeVector::operator()(CompileTimeColon);
    RunTimeVector RunTimeVector::operator()(RunTimeColon);
    

    This pattern would be necessary for any operation to interoperate between the run-time and compile-time types of Vector and colon.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.