dimanche 29 novembre 2020

C++ | Connecting source files issue

Edit: Ok, so whole code is here: https://www.github.com/LatekVon/RavenOs I have couple seperate source files i want to link, but it just doesn't work. In single file i have couple functions, and some are recognized by compiler and some are not. Just so you know, this program worked when being a singular mess, but now it doesnt. Compiler even throws error saying, that stringSplit is not recognized, even thought countString is. Every single file has same #include files. #include "header.h" is in every file, and in header itself, every function is listed. Here is header, the shortest file:

#ifndef KERNEL_H
#define KERNEL_H

//is this really needed?
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector> 
#include <deque>
#include <cstdlib>
#include <chrono>

using namespace std; //added this after edit!

#define VGA_ADDRESS 0xB8000
#define WHITE_COLOR 15
typedef unsigned short UINT16;//16bit value

//Pub values
string user_name;

//VGA
static UINT16 VGA_DefaultEntry(unsigned char ch_to_print);
UINT16 *TERMINAL_BUFFER;

//UTILS
int countString(string str);
string splitString(string str, int index);
void log(string str);

//PARSER
bool programParser(string program, string wholeString);

#endif

And im sure, i can fix problem for myself after putting here just one of the files, as error occurs in every file, and files like parser.cpp are just gonna add mess to this problem. Only info about parser.cpp you probably need to know, is that it calls utils.cpp and both of these files, as i said earlier, have #include "header.h" file. Here is 'utils.cpp':

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector> 
#include <deque>
#include <cstdlib>
#include <chrono>

#include "kernel.h"

using namespace std;

//a 16bit rendering of characters
static UINT16 VGA_DefaultEntry(unsigned char ch_to_print){
    //left shift converts color to character value pixel encoding
    return (UINT16)ch_to_print | (UINT16)WHITE_COLOR << 8;
}

//will split strings and return word from zero

int countString(string str){
    cout << "entered countString() module" << endl;
    stringstream countString_stream(str);
    int countString_finalCount = 0;
    string counterString = "";

    while(countString_stream >> counterString){ 
        countString_finalCount++;
    }

    return countString_finalCount;
}
string splitString(string str, int index){ /*index from 0*/
    cout << "entered splitString() module" << endl;
    if(countString(str) <=  1){//if 0 or 1 then return input//
        return str;
    }
    stringstream splitString_stream(str);
    string spliced = "";

    for(int i = 0; i <= index && splitString_stream.rdbuf()->in_avail() != 0; i++){
        splitString_stream >> spliced;
        cout << "debug: splitString: spliced = " << spliced << endl;
    }
    splitString_stream.clear();
    cout << "debug: splitString: output = " << spliced << endl;
    return spliced;
}
void log(string str){
    static string entryID = "["+ chrono::steady_clock::now +"]"+ user_name + ": ";
    ofstream logFileO;
    ifstream logFileI;
    if(logFileO.is_open() == false){
        if(logFileI.is_open() == false){
            logFileI.open("logs.data");
        }
        logFileO.open("logs.data");
        string tempString;
        deque<string> tempDeque;
        while(getline(logFileI,tempString){
            tempDeque.push_back(tempString);
        }
        while(deque.empty() == false){
            logFileO << tempDeque.front;
            tempDeque.pop_front;
        }
    }
    logFileO << entryID << str;
}

As last note: As you can see, i have all #include files also in header, i have no idea if its right thing to do, but it didnt work either way. Also, compiler command i used is: g++ -o kernel utils.cpp main.cpp parser.cpp I didnt inlclude header, as it threw even more errors regarding functions not being initialized/declared (i mistake these two). After edit, decided to also put this code here:

//counting args
    int argAmount = countString(wholeString) - 1;//this includes input but not program name
    int argAmountActual = 0;//only -x arguments
    int argAmountInputs = argAmount;
        
    //cant just check last arg for input as it could be in centre
        
    for(int i = 0; i <= argAmount; i++){
        string tempString = "";
        tempString = splitString(wholeString, i);
        cout << "debug: programParser: tempString = " << tempString << endl;
        if(tempString.size() != 0){
            if(tempString.at(0) == '-'){
                argAmountActual++;
            }
        }
    }

Its very important, as countString can be called and recognized, but splitString cannot. I know that this code is flawed, but im more intrested why does countString work, and splitString doesn't. Cheers

Aucun commentaire:

Enregistrer un commentaire