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

  • SEARCH
  • Home
  • 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 7506397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:14:56+00:00 2026-05-29T22:14:56+00:00

This seems so simple, but I cannot figure out what’s wrong. I’m implementing the

  • 0

This seems so simple, but I cannot figure out what’s wrong. I’m implementing the C++ vector class (only for int, not a template), and functions with iterator templates or typedefs are giving me these errors on compile:

Undefined symbols:
  "void vectorInt::assign<int>(int, int)", referenced from:
      _main in ccNVdR23.o
  "void vectorInt::assign<int*>(int*, int*)", referenced from:
      _main in ccNVdR23.o
      _main in ccNVdR23.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Important parts for the source files are:

vectorInt.h

#include <cstdlib>
#include <stdexcept>

typedef unsigned int size_type;

class vectorInt {
private:
    int* array;
    size_type current_size;
    size_type current_capacity;
public:
    .
    .
    .
    template <class InputIterator>
        void assign(InputIterator first, InputIterator last);
    void assign(size_type n, const int u);
};

#endif // VECTORINT_H

vectorInt.cpp

#include vectorInt.h
.
.
.
template <class InputIterator>
void vectorInt::assign(InputIterator first, InputIterator last) {
    clear();
    InputIterator it = first;
    int count = 0;
    while(it++ != last) {
        count++;
    }

    reserve(count);
    while(first != last) {
        this->push_back(*first++);
    }
}

void vectorInt::assign(size_type n, const int u) {
    clear();
    reserve(n);

    for(int i=0; i<(int)n; i++)
        push_back(u);
}

main.cpp

#include <cstdlib>
#include <stdexcept>
#include <iostream>
#include "vectorInt.h"

using namespace std;

int main(int argc, char** argv) {
    vectorInt first;
    vectorInt second;
    vectorInt third;

    first.assign(7, 100);

    vectorInt::iterator it;
    it = first.begin()+1;
    second.assign(it, first.end()-1); // the 5 central values of first

    int myints[] = {1776,7,4};
    third.assign(myints, myints+3);   // assigning from array.  

    return 0;
}

FYI: I know the main method uses the vectorInt::iterator, but that is not the problem and hence I didn’t include it in the source code.

  • 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-05-29T22:14:57+00:00Added an answer on May 29, 2026 at 10:14 pm

    Template code gets two phase compilation. Phase one includes only basic syntax check. The second phase, which is dependent on type T, gets full compilation from the compiler. The class/function will get instantiated based on type T, and the second phase compilation will run against that type.

    Since your code (implementation) is in .cpp file, it would get only first phase compilation, and thus it will not be included in the translation unit – No object file would be generated.

    For templates, you must allow the compiler to compile the entire code. And for the same you need to put entire implementation in header file only. You may also #include respective .cpp file just after the class declaration.

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

Sidebar

Related Questions

this seems simple but I cannot figure out how to do it or the
This seems simple but I can't figure it out. I receive post data in
This seems like an extremely easy problem but alas I cannot figure it out
This seems painfully simple, but I can't work out how to do it: I
This seems like it should be really simple, but I'm unable to figure this
I cannot figure this one out, but I cannot seem to get android to
Ok this should be easy but I can NOT figure it out. I have
I'm sure this is dead simple, but I can't seem to figure it out.
I apologize if this is a newbie question, but I cannot figure out why
This seems to be an absurdly simple question but Google and Stack Overflow searches

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.