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 8710543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:39:09+00:00 2026-06-13T04:39:09+00:00

So I have 3 files: main.cpp, sort.h and sort.cpp. Here they are: sort.h: #include

  • 0

So I have 3 files: main.cpp, sort.h and sort.cpp. Here they are:

sort.h:

#include <iostream>
class sorter
{
    public:
        void bubble(int*, int);
        void selection(int*, int);
        void insertion(int*, int);
        void bubble(int*, int, bool);
        void selection(int*, int, bool);
        void insertion(int*, int, bool);
        void print_arr(int*, int);
};

sort.cpp:

#include "sort.h"
void sorter::bubble(int* arr, int size)
{
    this->bubble(arr, size, false);
}
void sorter::bubble(int* arr, int size, bool verbose)
{
    ...
}

void sorter::selection(int* arr, int size)
{
    this->selection(arr, size, false);
}
void sorter::selection(int* arr, int size, bool verbose)
{
    ...
}

void sorter::insertion(int* arr, int size)
{
    this->insertion(arr, size, false);
}
void sorter::insertion(int* arr, int size, bool verbose)
{

}

void sorter::print_arr(int* arr, int size)
{
    ...
}

main.cpp

// Standard C++ lib
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>

// Mina classes etc.
#include "sort.h"

int main()
{
    system("title Uppgift 4, sorteringsalgoritmer. © Axel Latvala 2012");
    bool machineinput = false;
    int size = 0;
    int* pArr;
    std::string input = "";
    bool verbose = false;
    std::stringstream inputStream("");

    while(true)
    {
        int choice = -1;
        std::cout << "Vill du köra i verbose mode? (1 = ja, 0 = nej): ";
        getline(std::cin, input);

        inputStream.str(input);
        if(inputStream >> choice)
        {
            if(choice == 1)
            {
                verbose = true;
                    break;
                }
            else if(choice == 0)
        {
            verbose = false;
            break;
        }
        std::cout << "Välj antingen 1 eller 0: ";
    }
    else
    {
        std::cout << "Välj antingen 1 eller 0: ";
    }
}
system("cls");
std::cout << "Hur många element vill du mata in?\n";
while(true)
{
    std::cout << "Antal element(eller 0 för talföljd): ";
    getline(std::cin, input);
    inputStream.str("");
    inputStream.clear();
    inputStream.str(input);
    // String -> Int
    if(inputStream >> size)
    {
        if(size == 0)
        {
            machineinput = true;
            std::cout << "Du valde 0, vi genererar en talföljd. Hur många element?\n";
            std::cout << "Antal element:";
            int n = 0;
            while(true)
            {
                getline(std::cin, input);
                inputStream.str("");
                inputStream.clear();
                inputStream.str(input);
                if(inputStream >> n)
                {
                    if(n > 0)
                    {
                        break;
                    }
                    else
                    {
                        std::cout << "Antal element måste vara > 0.\n:";
                    }
                    std::cout << "Antal element måste vara integer.\n:";
                }
            }
            size = n;
            pArr = new int[size];
            std::cout << "1) Slumpmässig\n";
            std::cout << "2) n, n-1, n-2 ... n-(n-1)\n";
            std::cout << "3) n+1, n+2 ... n+(n-1)\n";
            std::cout << "Val: ";
            bool ready = false;
            while(!ready)
            {
                ready = true;
                getline(std::cin, input);
                inputStream.str("");
                inputStream.clear();
                inputStream.str(input);
                int x = -1;
                if(inputStream >> x)
                {
                    switch(x)
                    {
                    case 1:
                        for(int x = 0;x<=n-1;x++)
                        {
                            pArr[x] = rand()+1;
                        }
                        break;
                    case 2:
                        for(int x = 0;x<=n-1;x++)
                        {
                            pArr[x] = n-x;
                        }
                        break;
                    case 3:
                        for(int x = 0;x<=n-1;x++)
                        {
                            pArr[x] = x+1;
                        }
                        break;
                    default:
                        std::cout << "Välj ett alternativ bland alternativen 1-3.\n:";
                        ready = false;
                        break;
                    }
                }
                else
                {
                    ready = false;
                    std::cout << "Välj ett alternativ bland alternativen 1-3.\n:";
                }
            }
        }
        else
        {
            pArr = new int[size];
            input = "";
            system("cls");
            std::cout << "Antal element: " << size << ", godkännt.\n";
        }
        break;
    }
    else
    {
        std::cout << "\"" << input << "\" duger inte som antal element. Försök igen.\n";
    }
}
if(!machineinput)
{
    std::cout << "Var god och mata in elementen när de frågas efter.\n";
    for(int i=0;i<=size-1;i++)
    {
        while(true)
        {
            std::cout << "Mata in element nummer " << i+1 << ": ";
            getline(std::cin, input);

            std::stringstream inputStream(input);
            if(inputStream >> pArr[i])
            {
                std::cout << "Element " << i+1 << " = " << pArr[i] << "\n";
                break;
            }
            else
            {
                std::cout << "Elementet måste vara av typ integer.\n";
            }
        }
    }
}
system("cls");
sorter Sort;
std::cout << "Start: ";
Sort.print_arr(pArr, size);
std::cout << "\n";
int* origArr;
origArr = new int[size];
for(int x = 0;x<=size-1;x++)
{
    origArr[x] = pArr[x];
}
std::cout << "Du har att välja mellan 3st sortetingsalgoritmer, vilka som är följande:\n";
std::cout << "1) Bubble\n";
std::cout << "2) Selection\n";
std::cout << "3) Insertion\n";

while(true)
{
    int ready = 0;
    int choice = -1;
    std::cout << "Välj sorteringsalgoritm: ";
    getline(std::cin, input);
    std::stringstream inputStream(input);

    if(inputStream >> choice)
    {
        sorter* sortmachine = new sorter;
        ready = 1;
        std::cout << "Valde alternativ " << choice;
        std::stringstream tmptitle;
        char buffer[70];
        switch (choice)
        {
        case 1:
            // Bubble
            std::cout << ", bubble.\n\n";
            sprintf_s(buffer, "title Bubble sortering, %d element. © Axel Latvala\n", size);
            system(buffer);
            sortmachine->bubble(pArr, size, verbose);
            break;
        case 2:
            // Selection
            sprintf_s(buffer, "title Selection sortering, %d element. © Axel Latvala\n", size);
            system(buffer);
            std::cout << ", selection.\n\n";
            sortmachine->selection(pArr, size, verbose);
            break;
        case 3:
            // Insertion
            sprintf_s(buffer, "title Insertion sortering, %d element. © Axel Latvala\n", size);
            system(buffer);
            std::cout << ", insertion.\n\n";
            sortmachine->insertion(pArr, size, verbose);
            break;
        default:
            ready = 0;
            std::cout << ", okännt alternativ. Använd alternativen 1-3.\n\n";
            break;
        }
        if(ready == 1)
        {
            break;
        }
        delete sortmachine;
        sortmachine = 0;
    }
    else
    {
        std::cout << "\"" << input << "\" är ett okännt alternativ. Använd alternativen 1-3.\n";
    }
}
std::cout << "Resultat: ";
Sort.print_arr(pArr, size);
std::cout << "\n";
std::cout << "Tryck enter för att sluta programmet.\n© Axel Latvala 2012\n";
getline(std::cin, input);
return 0;
}

Compiles and runs fine on windows but on linux I get these errors:

[akke@eresk: ~/cpp]$ c++ -I /home/akke/cpp main.cpp
/tmp/ccJsJR5a.o: In function `main':
main.cpp:(.text+0x9bc): undefined reference to `sorter::print_arr(int*, int)'
main.cpp:(.text+0xbcc): undefined reference to `sorter::bubble(int*, int, bool)'
main.cpp:(.text+0xc2b): undefined reference to `sorter::selection(int*, int, bool)'
main.cpp:(.text+0xc87): undefined reference to `sorter::insertion(int*, int, bool)'
main.cpp:(.text+0xd93): undefined reference to `sorter::print_arr(int*, int)'
collect2: ld returned 1 exit status

This is my first time compiling on linux, what have I done wrong?

  • 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-13T04:39:10+00:00Added an answer on June 13, 2026 at 4:39 am

    Your compilation command is incorrect, you’re not including sort.cpp. Try:

    c++ -I /home/akke/cpp main.cpp sort.cpp
    

    Going forward, you might want to create a simple make file, this should work with gmake which is common on most Linux/UNIX machines (untested!)

    INCLUDES=
    CC=/usr/bin/c++
    CPPFLAGS=
    LIBS=
    DBG=-g
    
    .PHONY: all
    
    all:  sorter
    
    sorter: main.o sort.o
        $(CC) $+ -o sorter
    
    main.o: main.cpp main.h sort.h
        $(CC) -c $(DBG) $(INCLUDES) $(CPPFLAGS) $+
    
    sort.o: sort.cpp sort.h
        $(CC) -c $(DBG) $(INCLUDES) $(CPPFLAGS) $+
    

    Then you can just type make every time you change a cpp file and it won’t have to recompile every file like it does with the all-in-one command above.

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

Sidebar

Related Questions

I have three files, a main .cpp file: #include <stdio.h> #include myClass.h int main()
I have 4 files: shared.h main.cpp something.h something.cpp shared.h: #ifndef SHARED_H #define SHARED_H int*
I have three files: 1. Joy.h class Joy { public: void test(); }; 2.
I have 3 files: SilverLines.h SilverLines.cpp main.cpp At SilverLines.cpp I have: #include SilverLines.h. When
I have 3 files main.cpp a.h b.h main.cpp includes both a.h and b.h b.h
Can't understand what is a problem here: I have got main.cpp file where I
Here I am trying to implement the jQuery I have two main files one
I have three files: Main, slist.h and slist.cpp can be seen at http://forums.devarticles.com/c-c-help-52/confused-why-i-am-getting-c2143-and-c1010-error-259574.html I'm
I have 4 files message.proto udp.h udp.cpp main.cpp message.proto is a google protocol buffer
I have 3 files in my program: App_interface.h, App_interface.cpp, main.cpp. Im trying to compile

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.