Jump to content


Photo

Programmer shizzies


285 replies to this topic

#1 Daemon9623

Daemon9623

    Shizz Overlord

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 18,932 posts
  • Location:Middle Earth

Posted 14 April 2012 - 02:28 PM

Any programmer shizzies want to help a brother out?

I'm running into a serious roadblock with this assignment for my C++ class. The chapter is on operator overloading and the assignment is creating two matrices and then adding, subtracting and multiplying them with overloaded operators. Every time I compile I'm hit with a slew of errors that I can't seem to get around. If anyone wants to take a look at my implementation file and see maybe what I'm doing wrong I'd greatly appreciate it.

MatrixClass.H

If you don't want to download it the code is here also:
Spoiler

  • 1

The music I make.

Simpsons Shizzposting

TAKE IT TO THE ZELDA BREATH OF THE WILD THREAD, IDIOT


#2 mooniniteG

mooniniteG

    Sleeveless

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 12,637 posts
  • Location:Seattle, WA

Posted 14 April 2012 - 02:47 PM

Give me a few minutes and I'll check it out.
  • 1

T-Square_Bulbasaur_Bowie_zps3zwvagzu.jpg


#3 mooniniteG

mooniniteG

    Sleeveless

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 12,637 posts
  • Location:Seattle, WA

Posted 14 April 2012 - 03:00 PM

A couple of things that look wrong:

Ln 19 - The compiler is expecting matrix to be a type, but it's not declared anywhere. From your comment, this is supposed to be your ctor. Change it to:

void matrixType()

I'm not sure I follow what you're trying to do with the [][] here.

Ln 21/23/25 - Again, you're using matrix instead of matrixType.

Ln 60 - There's an end to a C-style comment (i.e. */) that isn't matched with a starting /*; delete it.

Ln 71 - Need to add matrixType:: before getSize since it's a member function.

Fixing these should help clear up any remaining warnings and errors.
  • 1

T-Square_Bulbasaur_Bowie_zps3zwvagzu.jpg


#4 juef

juef

    Shizz Captain

  • Members
  • PipPipPipPipPipPipPip
  • 1,028 posts
  • Location:Quebec, Canada

Posted 14 April 2012 - 03:00 PM

Looked quickly, but I believe there's some places where you probably meant 'matrixType' rather than 'matrix'. But hey, I haven't touched C++ in a while, so you'd probably be best wait for mooniniteG's reply!

[EDIT] Sniped! :)
  • 2

#5 Rize

Rize

    Shizz Gawd

  • Members
  • PipPipPipPipPipPipPipPipPipPip
  • 11,118 posts
  • Location:Baton Rouge

Posted 14 April 2012 - 03:29 PM

If you still have errors you can't fix, post the compiler errors along with the source code!
  • 1

#6 Daemon9623

Daemon9623

    Shizz Overlord

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 18,932 posts
  • Location:Middle Earth

Posted 14 April 2012 - 03:36 PM

A couple of things that look wrong:

Ln 19 - The compiler is expecting matrix to be a type, but it's not declared anywhere. From your comment, this is supposed to be your ctor. Change it to:

void matrixType()

I'm not sure I follow what you're trying to do with the [][] here.

Ln 21/23/25 - Again, you're using matrix instead of matrixType.

Ln 60 - There's an end to a C-style comment (i.e. */) that isn't matched with a starting /*; delete it.

Ln 71 - Need to add matrixType:: before getSize since it's a member function.

Fixing these should help clear up any remaining warnings and errors.

Thanks! I, unfortunately, don't have my compiler on hand at the moment since I'm at work, but I fixed these in notepad and will check them when I get home.

My naming convention was flawed initially as I called matrixType just matrix and it sounded more like a variable than a class. When I changed it I missed changing a few of them throughout the program.
The purpose behind matrix[][] was that I was trying to declare the constructor for matrixType, but was having a severe brainfart when it came to doing so with 2 dimensional arrays.

I set the default constructor for matrixType to:

//Constructor
matrixType::matrixType()
{
row = 0;
col = 0;

matrix[row][col];
}


Which I think is correct, since row and col were delcared as private ints the constructor will initialize them to 0 and use them as the values for the matrix. Then when the setSize method is called with the corresponding matrix passed as a parameter it will then change row and col to the user-specified sizes. Right?

If you still have errors you can't fix, post the compiler errors along with the source code!

Yeah, I will if I run into problems I can't fix on my own. Didn't have the compiler on this computer so I'm just adjusting the code in notepad.
  • 0

The music I make.

Simpsons Shizzposting

TAKE IT TO THE ZELDA BREATH OF THE WILD THREAD, IDIOT


#7 auriplane

auriplane

    Shizz Captain

  • Members
  • PipPipPipPipPipPipPip
  • 1,322 posts

Posted 14 April 2012 - 03:40 PM

Note that arrays aren't pointers.
  • 0

#8 mooniniteG

mooniniteG

    Sleeveless

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 12,637 posts
  • Location:Seattle, WA

Posted 14 April 2012 - 03:45 PM

//Constructor
matrixType::matrixType()
{
row = 0;
col = 0;

matrix[row][col];
}


Which I think is correct, since row and col were delcared as private ints the constructor will initialize them to 0 and use them as the values for the matrix. Then when the setSize method is called with the corresponding matrix passed as a parameter it will then change row and col to the user-specified sizes. Right?

matrix[row][col] doesn't set the size of the matrix, it will return the value at that index. You just have matrix defined as a pointer to a pointer to an int and it's never initialized to anything meaninful so deferencing it is going to result in undefined behavior.

If matrix doesn't need to have its sized changed, I'd hardcode the size to whatever is necessary for your assignment. Otherwise I'd template the size of the matrix, but you're probably not at the point of using templates yet.
  • 0

T-Square_Bulbasaur_Bowie_zps3zwvagzu.jpg


#9 juef

juef

    Shizz Captain

  • Members
  • PipPipPipPipPipPipPip
  • 1,028 posts
  • Location:Quebec, Canada

Posted 14 April 2012 - 03:55 PM

My naming convention was flawed initially as I called matrixType just matrix and it sounded more like a variable than a class.

I believe it is considered a good coding practice to name a class with its first letter capitalized (e.g. Matrix). It could even have a member named 'matrix' with no problem.
  • 0

#10 Nick The Newbie

Nick The Newbie

    Shizz JediMaster

  • Members
  • PipPipPipPipPipPipPipPip
  • 4,439 posts

Posted 14 April 2012 - 04:01 PM

We all good here, or do you still need help?
  • 0

#11 Daemon9623

Daemon9623

    Shizz Overlord

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 18,932 posts
  • Location:Middle Earth

Posted 14 April 2012 - 04:17 PM


//Constructor
matrixType::matrixType()
{
row = 0;
col = 0;

matrix[row][col];
}


Which I think is correct, since row and col were delcared as private ints the constructor will initialize them to 0 and use them as the values for the matrix. Then when the setSize method is called with the corresponding matrix passed as a parameter it will then change row and col to the user-specified sizes. Right?

matrix[row][col] doesn't set the size of the matrix, it will return the value at that index. You just have matrix defined as a pointer to a pointer to an int and it's never initialized to anything meaninful so deferencing it is going to result in undefined behavior.

If matrix doesn't need to have its sized changed, I'd hardcode the size to whatever is necessary for your assignment. Otherwise I'd template the size of the matrix, but you're probably not at the point of using templates yet.

Templates are actually involved in the next part of the next assignment and I'm reading through that section now. I'm guessing I should just set the matrices to 2x2 but I'm pretty sure the prof wants the sizes to be adjustable. Or do I need to overload the [] operator so it can be a nonconstant array? For some reason when dealing with arrays I feel like my brain melts out of my ear.
  • 0

The music I make.

Simpsons Shizzposting

TAKE IT TO THE ZELDA BREATH OF THE WILD THREAD, IDIOT


#12 mooniniteG

mooniniteG

    Sleeveless

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 12,637 posts
  • Location:Seattle, WA

Posted 14 April 2012 - 06:39 PM

Typically you'd want a 3x3 or a 4x4 matrix. Unfortunately you can't just have an adjustable size array. You do have a few options though:

1) Template your class to take in the array size. You could either take in 1 size or different sizes for the rows or columns, but I'm guessing you want a "square' matrix.

2) Dynamically allocate memory for the matrix on the heap using new[] and delete[].

3) Use an STL collection like a vector.

Can't say which one is the right option without knowing the specific requirements of the assignment. The best option is mostly like #1. Something like this should work:

template<int T>
class MatrixType
{
...stuff...
private:
int matrix[T][T];
};

Declare a 3x3 matrix like this:

MatrixType<3> myMatrix;

I don't use C++ very frequently these days, so my syntax could be wrong here. There should be plenty of references online if you need to use them.
  • 1

T-Square_Bulbasaur_Bowie_zps3zwvagzu.jpg


#13 Daemon9623

Daemon9623

    Shizz Overlord

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 18,932 posts
  • Location:Middle Earth

Posted 15 April 2012 - 07:53 AM

I see, I'm going to have to try and work that out. Just went through the templates section so that might help too.

Speaking of, the second half of the assignment is calling for me to modify some previously given code by adding a search function. I put them in a zip file here and maybe I could get a little feedback? It's supposed to search through a list of ints and a list of strings and find a match.

When I call the search function in the main program I get hit with this error for the string:
error: no matching function for call to 'listType<newString>::search(listType<newString>&, int, std::string&)'

and when I call the search function for the the int search I get this:
error: no matching function for call to 'listType<int>::search(listType<int>&, int, int&)'

Which leads me to believe that the parameters I input when I'm calling the function don't match the parameters of the function definition, but I'm brainfarting when it comes to where the discrepancy is.
  • 0

The music I make.

Simpsons Shizzposting

TAKE IT TO THE ZELDA BREATH OF THE WILD THREAD, IDIOT


#14 Daemon9623

Daemon9623

    Shizz Overlord

  • Members
  • PipPipPipPipPipPipPipPipPipPipPip
  • 18,932 posts
  • Location:Middle Earth

Posted 27 April 2012 - 09:45 PM

NEW QUESTION (that should be easier to answer):

Below are three functions, each for a different sorting method (bubble, selection and insertion). You'll notice two unused int variables numComps and numAssigns that are both initialized to 0. What I need to do is to have numComps increase by one for every comparison made and numAssigns increase by one for each assignment made. Then I need to output the results. I know how to increase them by one with numComps++ and numAssigns++ and then output it with cout << numComps << " ::: " << numAssigns; at the end of the loops, however I'm having a hard time figuring out where to put numComps++ and numAssigns++. It seems like wherever I do it just goes to the max number of items in the array or sits at one. Any help?

Spoiler

  • 0

The music I make.

Simpsons Shizzposting

TAKE IT TO THE ZELDA BREATH OF THE WILD THREAD, IDIOT


#15 Nick The Newbie

Nick The Newbie

    Shizz JediMaster

  • Members
  • PipPipPipPipPipPipPipPip
  • 4,439 posts

Posted 28 April 2012 - 03:55 AM

NEW QUESTION (that should be easier to answer):

Below are three functions, each for a different sorting method (bubble, selection and insertion). You'll notice two unused int variables numComps and numAssigns that are both initialized to 0. What I need to do is to have numComps increase by one for every comparison made and numAssigns increase by one for each assignment made. Then I need to output the results. I know how to increase them by one with numComps++ and numAssigns++ and then output it with cout << numComps << " ::: " << numAssigns; at the end of the loops, however I'm having a hard time figuring out where to put numComps++ and numAssigns++. It seems like wherever I do it just goes to the max number of items in the array or sits at one. Any help?

Spoiler


Well, firstly, that second for loop in bubble sort needs to have curly braces around what it's looping. Regardless of how many statements are being looped, they should always be wrapped by curly braces, even it it's just one statement. Same goes for if additional lines under if statements. ANYWAY, that being said, for bubble sort, since you need to check the number of comparisons, then you need to increment it right before that if statement, hence the need for curly braces to wrap that second for loop. On the phone, so it's a bitch to type, will look at the others later.
  • 3



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users