• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Sharp

Member
I would not read too heavily into those numbers. Looking at averages alone is rarely useful, especially when you don't know the sample sizes.
 

BreakyBoy

o_O @_@ O_o
To be clear, I was joking. Mostly.

I do feel I am underpaid, but it has nothing to do with those figures.

The fact that it doesn't take location/job market into account is a big problem with depending on those numbers.
 

GaimeGuy

Volunteer Deputy Campaign Director, Obama for America '16
Cant figure out how to fix this programming issue I'm having.
So basically I have to remove items from a vector if they are present in the other vector. Problem is, If I actually delete anything, the program goes bonkers and stops working. I know this is most likely caused by me deleting an item from the vector thats size is tied to a for loop. Unfortunately I cant figure out a way to make this work.
Code:
void dispatch(Order *disp)
    {
        bool mistake = true;
        for(int j = 0; j < disp->getSize(); j++)
        {

            for(int k = 0; k<stocked.size(); k++)
            {
                if(disp->getOrderName(j).compare(stocked[k]->giveName()) == 0)
                {
                    stocked[k] = NULL;
                    //mistake = false;
                    return;
                }
            }
         
            if(mistake)
                {
                    cout<<"An Error occured.\n";
                    return;
                }
        }
    }
Nevermind the mistake part in the code.

Edit: Apparently adding k=stocked.size(); before deleting an element did the job. Also tried break after deleting but that didn't help. Not sure if this is the best courese of action, but it works...
Double Edit: Well that actually did not work as intended. I still need advice.
Threes the charm: Got a solution. Apparently had to check if an element was NULL before comparing it to something. Otherwise it went bonkers.

Loop over the container using an iterator.
 

Quasar

Member
This really is a C# for Dummies question.

So I'm playing around with class libraries and inheritance.

I have a class (called Student) in a class library (called testLibrary).

Anyway I've tried creating a child class of Student called AcademicRecord.

Anyway whenever I try and call AcademicRecord's default constructor I get a 'AcademicRecord() is inaccessible due to its protection level'. I'm wondering why that is.

I can create instances of Student just fine so the class library is accessible just fine.

The class library looks like this:

Code:
namespace testLibrary
{
    public class Student
    {
        public int StudentID { get; set; }
        public string StudentFirstName { get; set; }
        public string StudentLastName { get; set; }

        public Student()
        {
            this.StudentID = 0;
            this.StudentFirstName = "FirstName";
            this.StudentLastName = "LastName";
            
        }

        public Student(int studentID, string firstName, string lastName)
        {
            this.StudentID = studentID;
            this.StudentFirstName = firstName;
            this.StudentLastName = lastName;
        }
    }
}

And the child class looks like this:

Code:
using testLibrary;

namespace WindowsFormsApplication3
{
    public class AcademicRecord: Student
    {
        public string degree { get; set; }
        public string major { get; set; }

        AcademicRecord()
        {
            Console.WriteLine("Child Constructor.");
        }   
    }
}

And the app code looks like this:

Code:
using testLibrary;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        ArrayList studentList = new ArrayList();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            Student test = new Student();
            Student test2 = new Student { StudentID = 2, StudentFirstName = "Jason", StudentLastName = "Smith" };
            Student test3 = new Student(3, "Mary", "Jones");
            studentList.Add(test);
            studentList.Add(test2);
            studentList.Add(test3);

            AcademicRecord ar1 = new AcademicRecord();
        }
 

poweld

Member
This really is a C# for Dummies question.

So I'm playing around with class libraries and inheritance.

I have a class (called Student) in a class library (called testLibrary).

Anyway I've tried creating a child class of Student called AcademicRecord.

Anyway whenever I try and call AcademicRecord's default constructor I get a 'AcademicRecord() is inaccessible due to its protection level'. I'm wondering why that is.

I can create instances of Student just fine so the class library is accessible just fine.

The class library looks like this:

Prefix the constructor AcademicRecord() with "public"

Each method has a visibility level, either private, protected, or public (note: I don't know C#, but this is usually the case). C# defaults methods to private, meaning that only instances of that class can call that method.

Your constructor method needs to be public so that it can be called from outside the class.

On another note, this inheritance doesn't make sense. Whenever you have a class inherit from another, make sure you check that it follows the rule of "is a". Having "Ford" inherit from "CarManufacturer" makes sense, because Ford is a car manufacturer. An academic record is not a student. This would call for the student containing a member.
 

Quasar

Member
Prefix the constructor AcademicRecord() with "public"

Each method has a visibility level, either private, protected, or public (note: I don't know C#, but this is usually the case). C# defaults methods to private, meaning that only instances of that class can call that method.

Your constructor method needs to be public so that it can be called from outside the class.

On another note, this inheritance doesn't make sense. Whenever you have a class inherit from another, make sure you check that it follows the rule of "is a". Having "Ford" inherit from "CarManufacturer" makes sense, because Ford is a car manufacturer. An academic record is not a student. This would call for the student containing a member.


True. The child was just random stuff to use in understanding how it all works. Its not going to be used for anything. I guess it should have been more like Person->Student
 

tokkun

Member
Who did they interview in 2014 that is strictly a Perl coder?

The data is based on what skills were listed as requirements on the job posting. They are not necessarily exclusive. So that data represents the average salary of 500 or more jobs that listed Perl as a requirement (and may have listed other languages or technical skills as requirements).
 

Water

Member
C++ is the GOAT of programming languages.
The more you know about C++, the more hesitant you should be to use it.
My experience is overwhelmingly in C++ and I have even taught it, but unless I have specific reasons to use C++ for a new (serious) project, I wont. I'll sooner pick up another, better behaved language that I have almost no experience in.
My school started us off with C++.. I took nearly 2 years of C++ classes and ended up with a job where I have to learn Java and it feels so damn wordy to me.
I don't think you are wrong. Java is wordy and clunky, more so than any other language I've used. It's so bad that I feel C++ actually is cleaner and more expressive for hacking stuff together on a small scale; that's not a good comparison to end up in. Where C++ becomes painful is when it's written by poor programmers and/or put in a large system.

That said, one of the best coders I know used Java even for a lot of his free time hacking (neural networks, pattern recognition stuff). He was familiar and comfortable with it and that counts for a lot.

If I personally had to run code on JVM, I'd probably go and learn Clojure from scratch. Scala would be another option, but I have some reservations from when I've tried it briefly.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
I found this in my bookmarks from the project:

http://www.kylejlarson.com/blog/2011/fixed-elements-and-scrolling-divs-in-ios-5/

The project was a bit of an odd thing, it used an iPad hooked up to a monitor and the iPad was viewing a webpage in a UI webview, and clicks and scrolls that the user did on the iPad would be reflected on the monitor. I had to write bridging code that would go from JavaScript to Objective-C for some stuff.

In the end it didn't work as well as the client wanted because UIWebview performance was poop on iOS7.

Thanks for the link, and the help. This didn't end up solving my current issue though.

It seems as if iOS 8 is just not sending the proper touch events for paper.js to understand for it's built in functions. I may just end up hard coding touch events in and calling the paper functions manually instead to see if that fixes my issue.

Frustrating!

edit: I'm an idiot. Adding some code to prevent the default touch interaction on the page fixed the issue.
Code:
document.addEventListener('touchmove', function(event) {
    event.preventDefault();
}, false);
 

Vire

Member
Hey guys, simple problem that I need help with in Java that I have no idea how to solve.

Code:
System.out.println("Phone: "+sStudentPhoneNumber);

It's printing out phone numbers that it reads from a text file, the problem is that the phone number printed just outputs like this:

Phone: 5615672436

and I need it to print out like this

Phone (561) 567-2436

How do I insert the parenthesis/dashes and space into the existing string variable?
 

I make significantly less than the average starting salary for both of my primary languages. That's really sad.

Thanks for the link, and the help. This didn't end up solving my current issue though.

It seems as if iOS 8 is just not sending the proper touch events for paper.js to understand for it's built in functions. I may just end up hard coding touch events in and calling the paper functions manually instead to see if that fixes my issue.

Frustrating!

edit: I'm an idiot. Adding some code to prevent the default touch interaction on the page fixed the issue.
Code:
document.addEventListener('touchmove', function(event) {
    event.preventDefault();
}, false);

Glad to see you worked it out!
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
Glad to see you worked it out!

Me too. Although I pushed that change to my repo last night or the night before, and then got caught up in studying and never fully tested it.
So my app was running fine for over a day without me realizing it and still fretting over how to fix it.

Better than a day of it being broken without knowing though!

I still have a nice list of features and fixes I'd like to add in, but it's a nice feeling to have something out there and working that is useful. (We use it for whiteboarding at work now.)
 

pompidu

Member
Hey guys, simple problem that I need help with in Java that I have no idea how to solve.

Code:
System.out.println("Phone: "+sStudentPhoneNumber);

It's printing out phone numbers that it reads from a text file, the problem is that the phone number printed just outputs like this:

Phone: 5615672436

and I need it to print out like this

Phone (561) 567-2436

How do I insert the parenthesis/dashes and space into the existing string variable?

Stringbuilder should be what your looking for.

Edit: http://stackoverflow.com/questions/5884353/insert-a-character-in-a-string-at-a-certain-position
 
Not sure what your getting at? Situation not call for string builder? I also wouldnt clump phone numbers like that.

I meant that the performance difference between concatenation and using StringBuilder is so negligible that you shouldn't be considering performance as a factor at all. It's generally considered best not to worry about performance until you have a chance to profile your code and determine exactly where efficiency is needed.
 

Exuro

Member
I need some help with opengl/glsl. I'm trying to pass some variables over as uniforms to the shader but I want them to be type vec4. In opengl/main they are set up as float array[4]. How can I do this? I can only seem to set them up in the shader as the same type, which is very annoying for calculations. Is it possible to pass float arrays to the shader and have them be type vec4?

Also when I run this code it only spits out the first value. It works 100% as I can see the color change but only the first number is non zero as I change it.

Code:
GLint loc1 = glGetUniformLocation(shader.id(),"rgba");
glUniform1fv(loc1, 4, rgba);
	
glGetUniformfv(shader.id(), loc1, ptr2);
	
printf("#####%f, %f, %f, %f\n",ptr2[0], ptr2[1], ptr2[2], ptr2[3]);

This works just fine though...
Code:
GLint loc4 = glGetUniformLocation(shader.id(),"modelviewprojection");
glUniformMatrix4fv(loc4, 1, false, *modelviewprojection.m);
	
glGetUniformfv(shader.id(), loc4, ptr2);
	
for(int i = 0; i < 16; i++)
    printf("#####%f\n",ptr2[i]);
 

pompidu

Member
I meant that the performance difference between concatenation and using StringBuilder is so negligible that you shouldn't be considering performance as a factor at all. It's generally considered best not to worry about performance until you have a chance to profile your code and determine exactly where efficiency is needed.

Yeah I see what your saying. StringBuilder was just the first thing that popped in my head when reading his issue. I don't see his response yet so hopefully he figured it out.
 

hateradio

The Most Dangerous Yes Man
Hey guys, simple problem that I need help with in Java that I have no idea how to solve.

How do I insert the parenthesis/dashes and space into the existing string variable?
Checkout String.format or system.out.printf formatting.

You can use substring to get parts from a string or regular expressions to split the number into parts.

Code:
if (sStudentPhoneNumber.length == 10) {
   int area = sStudentPhoneNumber.substring(0, 3);
   int p1 = sStudentPhoneNumber.substring(3, 6);
   int p2 = sStudentPhoneNumber.substring(6, 10);

   system.out.printf("(%s) %s-%s%n", area, p1, p2); // %n adds a line break
} else {
   system.out.println("Invalid number?");
}
 
C++ gets syntactically hairy fast though. Never mind good code practices; it's difficult to get consistent code between people if there's no, or lax, code style guides.
In fairness, this is true no matter what language you pick. Consistent code style is completely arbitrary and completely essential.
 

Anustart

Member
Oh my. Program I'm working on just destroys my computer lol. Just hangs after I put in some object instantiations, so I'ma guess my problem is there somewhere.

It's something to do with these segments:

Code:
//if (byState.size() > 0)
                //{
                    //stateCensus.push_back(byState);
                //}
                //std::list<CityCensus> byState;

Code:
//CityCensus newCity(city, state, ten, eleven, twelve);
            //byState.push_back(newCity);
 

Slavik81

Member
I need some help with opengl/glsl. I'm trying to pass some variables over as uniforms to the shader but I want them to be type vec4. In opengl/main they are set up as float array[4]. How can I do this? I can only seem to set them up in the shader as the same type, which is very annoying for calculations. Is it possible to pass float arrays to the shader and have them be type vec4?

Also when I run this code it only spits out the first value. It works 100% as I can see the color change but only the first number is non zero as I change it.

Code:
GLint loc1 = glGetUniformLocation(shader.id(),"rgba");
glUniform1fv(loc1, 4, rgba);
	
glGetUniformfv(shader.id(), loc1, ptr2);
	
printf("#####%f, %f, %f, %f\n",ptr2[0], ptr2[1], ptr2[2], ptr2[3]);

This works just fine though...
Code:
GLint loc4 = glGetUniformLocation(shader.id(),"modelviewprojection");
glUniformMatrix4fv(loc4, 1, false, *modelviewprojection.m);
	
glGetUniformfv(shader.id(), loc4, ptr2);
	
for(int i = 0; i < 16; i++)
    printf("#####%f\n",ptr2[i]);
Code:
// compile & link shaders and get the program_id from OpenGL

GLuint location = glGetUniformLocation(program_id, "colors_variable_name_in_shader");
glUseProgram(program_id);
float color[4] = {1.f, 0.5f, 0.7f, 1.f};
glUniform4fv(location, 1, color);

// now you're ready to draw

Also, watch for any OpenGL errors you might have. I've said this a few times, but I love ApiTrace for checking if I have any OpenGL errors. It's pretty easy to build, too. On Linux at least... haven't tried on Windows.

Oh my. Program I'm working on just destroys my computer lol. Just hangs after I put in some object instantiations, so I'ma guess my problem is there somewhere.

It's something to do with these segments:

Code:
//if (byState.size() > 0)
                //{
                    //stateCensus.push_back(byState);
                //}
                //std::list<CityCensus> byState;
How are you using byState before it's declared?
 

Anustart

Member
Code:
// compile & link shaders and get the program_id from OpenGL

GLuint location = glGetUniformLocation(program_id, "colors_variable_name_in_shader");
glUseProgram(program_id);
float color[4] = {1.f, 0.5f, 0.7f, 1.f};
glUniform4fv(location, 1, color);

// now you're ready to draw

Also, watch for any OpenGL errors you might have. I've said this a few times, but I love ApiTrace for checking if I have any OpenGL errors. It's pretty easy to build, too. On Linux at least... haven't tried on Windows.


How are you using byState before it's declared?

byState is actually a global. Is my line there in that if block not just making a new object? I'm unfamiliar with c++.

Would putting that there be making a byState local and not just overwriting the global byState? I'm used to c# where you can just do byState = new list<>, but that seemingly doesn't work here. I'm probably just not seeing how to do that.
 

Slavik81

Member
byState is actually a global. Is my line there in that if block not just making a new object? I'm unfamiliar with c++.
Code:
//if (byState.size() > 0)
                //{
                    //stateCensus.push_back(byState);
                //}
                //std::list<CityCensus> byState; <-- this creates a new object called byState
You seem to be creating a second object called byState at function scope. All further references to byState in that function would be referring to the function-scope byState rather than the global one. That's called called 'shadowing'.

If you wanted to clear the existing byState, you'd do something more like this:
Code:
byState = std::list<CityCensus>();
(which creates a new list and assigns it to the existing one)

or
Code:
byState.clear();
(which just calls the method that empties the list)
 

squidyj

Member
I have a piece of code sort of like this
Code:
template<typename T, int m, int n>
Matrix<T, m, n> columnMatrix(vector<T,m>&&... vecs)
{
Matrix<T,m,n> ret;
//blah blah blah construct the matrix from the given column vectors
return ret;
}

Which clearly doesn't work since a call to columnMatrix does not give the compiler enough information to determine n. Obviously this is because it doesn't know how many arguments are in the vecs pack. I could explicitly state the n for each call of columnMatrix but I would prefer not to

I don't know how I can maintain the call syntax and be able to infer n.
 

Anustart

Member
Code:
//if (byState.size() > 0)
                //{
                    //stateCensus.push_back(byState);
                //}
                //std::list<CityCensus> byState; <-- this creates a new object called byState
You seem to be creating a second object called byState at function scope. All further references to byState in that function would be referring to the function-scope byState rather than the global one. That's called called 'shadowing'.

If you wanted to clear the existing byState, you'd do something more like this:
Code:
byState = std::list<CityCensus>();
(which creates a new list and assigns it to the existing one)

or
Code:
byState.clear();
(which just calls the method that empties the list)

Perfect! Fixed the memory problem as well :)
 

Slavik81

Member
I have a piece of code sort of like this
Code:
template<typename T, int m, int n>
Matrix<T, m, n> columnMatrix(vector<T,m>&&... vecs)
{
Matrix<T,m,n> ret;
//blah blah blah construct the matrix from the given column vectors
return ret;
}

Which clearly doesn't work since a call to columnMatrix does not give the compiler enough information to determine n. Obviously this is because it doesn't know how many arguments are in the vecs pack. I could explicitly state the n for each call of columnMatrix but I would prefer not to

I don't know how I can maintain the call syntax and be able to infer n.
Aren't varadic arguments handled at runtime? I don't think it's possible to specify n, because it depends on the varadic arguments (which could be staticly handled, but are not... they come from C, afterall).

That said, I don't know that much about either varadic arguments or the new parameter packs. The latter might have a way to do this, since I think it is compile-time...

EDIT: See: http://en.cppreference.com/w/cpp/language/parameter_pack
And: http://stackoverflow.com/questions/...er-pack-is-there-a-c0x-std-lib-function-for-t

EDIT: EDIT: I didn't notice before, but you refer to vecs as a pack. I'm not convinced it is. From my understanding, it needs to be a template argument to be a pack. I think without that it is a varadic function. See: http://stackoverflow.com/a/20407859/331041
 

KingDAVIN

Neo Member
Hey guys, so Im fresh out of college, and I have an interview setup next week for a position as an automation tester.

Can anyone give me any details on it? Does anyone here have experience in that position?

I did a 3 year diploma in Software Development, which covered c#, java, Oracle 11g, Sql Server 2012, html and php.

I have no real world experience in coding but I did do very well in my studies. I haven't designed test cases before.

Apologies if this is a bit off topic.
 

BreakyBoy

o_O @_@ O_o
Hey guys, so Im fresh out of college, and I have an interview setup next week for a position as an automation tester.

Can anyone give me any details on it? Does anyone here have experience in that position?

Any ideas on what they're automating? Build pipeline, networking, cloud infrastructure? Any ideas what languages, frameworks, tools they use?

If you don't, that's probably OK. If you're fresh out of school, they probably expect you to not know. It'll look good if you ask those questions though.
 

squidyj

Member
Aren't varadic arguments handled at runtime? I don't think it's possible to specify n, because it depends on the varadic arguments (which could be staticly handled, but are not... they come from C, afterall).

That said, I don't know that much about either varadic arguments or the new parameter packs. The latter might have a way to do this, since I think it is compile-time...

EDIT: See: http://en.cppreference.com/w/cpp/language/parameter_pack
And: http://stackoverflow.com/questions/...er-pack-is-there-a-c0x-std-lib-function-for-t

EDIT: EDIT: I didn't notice before, but you refer to vecs as a pack. I'm not convinced it is. From my understanding, it needs to be a template argument to be a pack. I think without that it is a varadic function. See: http://stackoverflow.com/a/20407859/331041

yeah referring to it as a pack was a mixup of terminology. A parameter pack itself doesn't solve my problem but I might be able to write a trait to solve the issue
 

Pinewood

Member
I cant figure out why I cant insert into a vector at a specified spot.
I have the vector declared here
vector <Item*> inCart;
And the troublesome bit of code here.
Code:
inCart.insert ((inCart.begin()+i), (n-count), *prod);
Where inCart.begin()+i is where I want to insert *prod n-count times.

E: Made a typo in the code. Duh. Works now.
 

Anustart

Member
Also, std::list is not the equivalent to C#'s List. std::list is a linked list, which is only rarely useful. You probably want std::vector.

Yeah, problem is professor stated not to use a vector or an array. In his instructions he stated that the amount of data was too much to put into a typical data structure, though my tests showed no problems holding 19,517 elements in a vector.

My code is seemingly dropping two elements though, as there's 19,515 elements showing in the list.
 
I am a bit stuck in my program. I am doing a Multi-List Ordered List (Linked List) with Java. Really sucks, actually.

So, I am getting this rather strange exception when running:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2
at SortedLinkedList.SortedLinkedList.getLength(SortedLinkedList.java:382)
at SortedLinkedList.SortedLinkedList.getObjectsForKey(SortedLinkedList.java:107)
at SortedLinkedList.Test.main(Test.java:63)

Not sure why I am getting -2, honestly.

Here's my Classes, for reference:
http://pastebin.com/HuSkMFJp
http://pastebin.com/0uTdFSGr
http://pastebin.com/PD0SBLhE

So basically, all I want to do right now is:
d. Display all flight information for a user inputted Flight # + Date &#8211; this will list all the airports visited on this flight

The getObjectsForKey method is where I am making my implementation. Just kind of looking for some direction as to how to debug this.
 
Also, I am trying to refine my techniques when it comes to methods, Objects, and classes. Anyone have any pointers or references to check out? Even if my intro classes, I can admit, I never got a HUGE understanding of the aforementioned. I know what they do, obviously, but they still trip me up from time to time.
 

Slo

Member
I am a bit stuck in my program. I am doing a Multi-List Ordered List (Linked List) with Java. Really sucks, actually.

So, I am getting this rather strange exception when running:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2
at SortedLinkedList.SortedLinkedList.getLength(SortedLinkedList.java:382)
at SortedLinkedList.SortedLinkedList.getObjectsForKey(SortedLinkedList.java:107)
at SortedLinkedList.Test.main(Test.java:63)

Not sure why I am getting -2, honestly.

Here's my Classes, for reference:
http://pastebin.com/HuSkMFJp
http://pastebin.com/0uTdFSGr
http://pastebin.com/PD0SBLhE

So basically, all I want to do right now is:
d. Display all flight information for a user inputted Flight # + Date – this will list all the airports visited on this flight

The getObjectsForKey method is where I am making my implementation. Just kind of looking for some direction as to how to debug this.

You have this at line 107 of SortedLinkedList.java:

int index = findHeader(firstNodeOnChain, numberOfHeaders, key);
int size = getLength(index);

If you do an .indexOf(String); and the String is not found, you'll get a result of -1

Later at line 382 you have this:

return numberOfEntriesOnChain[chain-1];

So you're probably trying to index the array at index -2.
 

Pinewood

Member
So in C++ and inheritance.

I have 2 classes, one is inherited from another, both contain a vector of the same name.

I have a function in the base class that accesses the vector and want to use it in a derived class function. However this does not work. If I copy the function to the derived class it works.

Is there lika way to make this work?
 

Slo

Member
So in C++ and inheritance.

I have 2 classes, one is inherited from another, both contain a vector of the same name.

I have a function in the base class that accesses the vector and want to use it in a derived class function. However this does not work. If I copy the function to the derived class it works.

Is there lika way to make this work?

I'm not much of a C++ guy, but it's probably one of two things:

1) The visibility of the function in the super class is private and thus not inherited but the sub class.

2) Since the vector name is overridden, the function that accesses that superclass's vector can't be applied to the overwritten vector in the sub class. I'm not sure if inheritance works this way in C++, so it may be a reach.

BTW, why do you have a vector of the same name in the sub class? Why not just use the one that's already there?
 
Top Bottom